bitmap工具,长截图及截图合并

1,927 阅读2分钟

长图截图

以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;
}