溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中如何給圖片添加水印

發(fā)布時間:2022-04-11 16:09:50 來源:億速云 閱讀:438 作者:iii 欄目:編程語言

這篇文章主要介紹了Android中如何給圖片添加水印的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Android中如何給圖片添加水印文章都會有所收獲,下面我們一起來看看吧。

Android 圖片添加水印的實現(xiàn)方法

手機端打水?。ㄎ淖趾蛨D片)使用的是Bitmap、Matrix和Canvas類的一些方法, 可以實現(xiàn)拉伸、旋轉(zhuǎn)、位移等等效果。 原理很簡單, 就是在畫布Canvas上繪制圖形、圖片、文字等等, 得到你想要的效果圖片。

 /*
   添加全屏斜著45度的文字
   /
  public static Bitmap drawCenterLable(Context context, Bitmap bmp, String text) {
    float scale = context.getResources().getDisplayMetrics().density;
    //創(chuàng)建一樣大小的圖片
    Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888);
    //創(chuàng)建畫布
    Canvas canvas = new Canvas(newBmp);
    canvas.drawBitmap(bmp, 0, 0, null);  //繪制原始圖片
    canvas.save();
    canvas.rotate(45); //順時針轉(zhuǎn)45度
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.argb(50, 255, 255, 255)); //白色半透明
    paint.setTextSize(100 scale);
    paint.setDither(true);
    paint.setFilterBitmap(true);
    Rect rectText = new Rect();  //得到text占用寬高, 單位:像素
    paint.getTextBounds(text, 0, text.length(), rectText);
    double beginX = (bmp.getHeight()/2 - rectText.width()/2) * 1.4;  //45度角度值是1.414
    double beginY = (bmp.getWidth()/2 - rectText.width()/2) * 1.4;
    canvas.drawText(text, (int)beginX, (int)beginY, paint);
    canvas.restore();
    return newBmp;
  }

使用44KB的png圖片驗證效率:

long begin = System.currentTimeMillis();
Bitmap destBmp = ImageUtil.drawCenterLable(this, sourBitmap, "某某公司專用");
long end = System.currentTimeMillis();
Log.d("brycegao", "打水印用時:" + (end-begin) + "毫秒");
mWartermarkImage.setImageBitmap(destBmp);

小米4手機輸出: D/brycegao: 打水印用時:69毫秒

使用3M字節(jié)的jpg圖片測試打水印,報OOM錯誤。

 java.lang.OutOfMemoryError: Failed to allocate a 467251212 byte allocation with 16767536 free bytes and 110MB until OOM
                                        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
                                        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
                                        at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:469)
                                        at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:501)

關(guān)于“Android中如何給圖片添加水印”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Android中如何給圖片添加水印”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI