1. 程式人生 > >bitmap工具,長截圖及截圖合併

bitmap工具,長截圖及截圖合併

長圖截圖

以scrollview為例子,擷取scrollview中的所有內容,如果是擷取單個view,方式一樣,bitmap寬高直接取view的寬高

/**
 * 獲取長截圖
 * @return
 */
public Bitmap getFullScreenBitmap() {
    int h = 0;
    Bitmap bitmap;
    for (int i = 0; i < scrollVew.getChildCount(); i++) {
        h += scrollVew.getChildAt(i).getHeight();
    }
    // 建立對應大小的bitmap
    bitmap = Bitmap.createBitmap(scrollVew.getWidth(), h,
            Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    scrollVew.draw(canvas);
    // 測試輸出
    saveBitmapToFile(bitmap);
    return bitmap;
}

截圖合併

/**
 * 將兩個bitmap合併成一個
 */
public static Bitmap newBitmap(Bitmap bit1, Bitmap bit2, int padding) {
    Bitmap newBit = null;
    int width = bit1.getWidth();
    if (bit2.getWidth() != width) {
        int h2 = bit2.getHeight() * width / bit2.getWidth();
        newBit = Bitmap.createBitmap(width, bit1.getHeight() + h2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBit);
        Bitmap newSizeBitmap2 = getNewSizeBitmap(bit2, width, h2);
        canvas.drawBitmap(bit1, 0, 0, null);
        canvas.drawBitmap(newSizeBitmap2, 0, bit1.getHeight() - padding, null);
    } else {
        newBit = Bitmap.createBitmap(width, bit1.getHeight() + bit2.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBit);
        canvas.drawBitmap(bit1, 0, 0, null);
        canvas.drawBitmap(bit2, 0, bit1.getHeight() - padding, null);
    }
    // 測試輸出
    saveBitmapToFile(newBit);
    return newBit;
}


/**
 * bitmap縮放
 */
public static Bitmap getNewSizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    float scaleWidth = ((float) newWidth) / bitmap.getWidth();
    float scaleHeight = ((float) newHeight) / bitmap.getHeight();
    // 取得想要縮放的matrix引數
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // 得到新的圖片
    Bitmap bit1Scale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
            true);
    return bit1Scale;
}

其他一些工具方法

測試圖片,儲存到指定路徑下

private void saveBitmapToFile(Bitmap bitmap) {
    String fullScreenPath = PhotoUtils.getAppSaveDirPath() + "/" + System.currentTimeMillis() + ".jpg";
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(fullScreenPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        if (null != out) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (IOException e) {
    }
}

 /**
 * 獲得檔案儲存目錄路徑
 *
 * @return
 */
public static String getAppSaveDirPath() {
    File file = Environment.getExternalStorageDirectory();
    File dir = new File(file, "/sdcard/photo/");
    if (!dir.exists()) {
        dir.mkdirs();
    }

    return dir.getAbsolutePath();
}

bitmap加蒙版

   /**
     * bitmap加蒙版
     * @param src
     * @param color
     * @return
     */
    public static Bitmap createMaskBitmap(Bitmap src, int color) {
        if (src == null) {
            return null;
        }
        int width = src.getWidth();
        int height = src.getHeight();
        //建立一個bitmap
        Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 建立一個新的和SRC長度寬度一樣的點陣圖
        //將該圖片作為畫布
        Canvas canvas = new Canvas(newb);
        Paint p = new Paint();
        p.setColor(color);
        //在畫布 0,0座標上開始繪製原始圖片
        canvas.drawBitmap(src, 0, 0, null);
        //在畫布上繪製水印圖片
        canvas.drawRect(0,0,width,height,p); // 以原始Canvas畫出一個矩形
        // 儲存
        canvas.save(Canvas.ALL_SAVE_FLAG);
        // 儲存
        canvas.restore();
        return newb;
    }

bitmap加水印

/**
 * 加水印
 */
public static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,int paddingLeft, int paddingTop) {
    if (src == null) {
        return null;
    }
    int width = src.getWidth();
    int height = src.getHeight();
    //建立一個bitmap
    Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 建立一個新的和SRC長度寬度一樣的點陣圖
    //將該圖片作為畫布
    Canvas canvas = new Canvas(newb);
    //在畫布 0,0座標上開始繪製原始圖片
    canvas.drawBitmap(src, 0, 0, null);
    //在畫布上繪製水印圖片
    canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
    // 儲存
    canvas.save(Canvas.ALL_SAVE_FLAG);
    // 儲存
    canvas.restore();
    return newb;
}