Android 圖片添加水印

拍照或者本地圖片添加水印,效果圖:
這裏寫圖片描述

添加水印方法:

/**
     * @param src   添加水印的圖片
     * @param title 水印文字
     * @return
     */
    public Bitmap createBitmap(Bitmap src, String title) {
        if (src == null) {
            return src;
        }
        Bitmap bitmap = src;
        // 獲取原始圖片的寬與高
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas mCanvas = new Canvas(newBitmap);
        // 往位圖中開始畫入src原始圖片
        mCanvas.drawBitmap(bitmap, 0, 0, null);
        // 開始加入文字
        if (null != title) {
            Paint textPaint = new Paint();

            textPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 48, getResources().getDisplayMetrics()));
            String familyName = "宋體";
            Typeface typeface = Typeface.create(familyName,
                    Typeface.NORMAL);
            textPaint.setTypeface(typeface);
            textPaint.setTextAlign(Paint.Align.CENTER);
            float textHeight = textPaint.descent() - textPaint.ascent();

            textPaint.setAntiAlias(true);
            textPaint.setColor(Color.BLACK);
            textPaint.setStrokeWidth(textHeight + dip2px(8));
            textPaint.setAlpha((int) (255 * 0.8));
            mCanvas.drawLine(0, h - (textHeight / 2 + dip2px(4)), w, h - (textHeight / 2 + dip2px(4)), textPaint); // 先畫背景。
            textPaint.setColor(Color.WHITE);
            mCanvas.drawText(title, w / 2, h - (textHeight / 2) + dip2px(20), textPaint);

        }
        mCanvas.save(Canvas.ALL_SAVE_FLAG);
        mCanvas.restore();
        bitmap.recycle();
        return newBitmap;
    }

水印添加完成之後,有需要的話,可以保存到本地。方法如下:

/**
     * 把圖片村保存在相應的文件當中
     *
     * @param pBitmap
     * @param fileName
     */
    public static void saveFile(Bitmap pBitmap, String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fileName);
            pBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            Log.i("lfq", "保存圖片到sdcard卡成功.");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

這裏的 fileName 要傳入文件的絕對路徑。
根據需要生成路徑的方法:

/**
     * 設置拍照圖片保存路徑
     *
     * @return
     */
    public static String mImageFileCachePath() {
        String s = Environment.getExternalStorageDirectory() + File.separator + "Android" + File.separator
                + "_cache" + File.separator;
        File file = new File(s);
        if (!file.exists()) {
            file.mkdirs();
        }
        return s;
    }

這裏要注意添加對應的讀寫權限。甚至有些需要添加創建文件夾權限。

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

dip2px 方法:

/**
     * 根據手機的分辨率從 dp 的單位 轉成爲 px(像素)
     */
    public int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

Question:
這裏遇到的一個問題,有些機型拍照之後圖片會旋轉,這個時候如果根據旋轉角度對圖片進行調整之後再處理,會出現內存溢出問題。還請各位大神指點。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章