Android给图片添加认证水印


public static Bitmap drawWatermarkToImage(Bitmap bitmap, Bitmap waterBitmap) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }
    bitmap = bitmap.copy(bitmapConfig, true);

    int rowNum = 3;
    int waterWidth = bitmap.getWidth();
    if (bitmap.getHeight() < bitmap.getWidth()) {
        waterWidth = bitmap.getHeight();
    }
    waterWidth = waterWidth / rowNum;
    waterBitmap = zoomImage(waterBitmap, waterWidth, waterWidth);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < rowNum; j++) {
            int x = i * waterWidth;
            int y = j * (bitmap.getHeight() / rowNum);
            canvas.drawBitmap(waterBitmap, x, y, paint);
        }
    }
    return bitmap;
}
 
  
public static Bitmap zoomImage(Bitmap bitmap, int newWidth, int newHeight) {
    // 获得图片的宽高
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    // 计算缩放比例
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // 取得想要缩放的matrix参数
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // 得到新的图片
    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}


你可能感兴趣的:(Android)