When we are doing the project sometimes need to add a watermark to the picture, Shuizhong today encountered such a problem, so engage in a tool category, posted directly call you on the line.
/** * Picture tool class * @author Cold water * */ public class ImageUtil { /** * Set watermark image in the upper left corner * @param Context * @param src * @param watermark * @param paddingLeft * @param paddingTop * @return */ public static Bitmap createWaterMaskLeftTop( Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) { return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft), dp2px(context, paddingTop)); } private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) { if (src == null) { return null; } int width = src.getWidth(); int height = src.getHeight(); // Create a bitmap Bitmap newb = Bitmap.createBitmap(width, height, Config.ARGB_8888);// Create a new and SRC A bitmap of the same length and width // Use this picture as a canvas Canvas canvas = new Canvas(newb); // On canvas 0,0 Start drawing the original picture on the coordinates canvas.drawBitmap(src, 0, 0, null); // Draw watermark pictures on canvas canvas.drawBitmap(watermark, paddingLeft, paddingTop, null); // Preservation canvas.save(Canvas.ALL_SAVE_FLAG); // storage canvas.restore(); return newb; } /** * Set watermark image in the bottom right corner * @param Context * @param src * @param watermark * @param paddingRight * @param paddingBottom * @return */ public static Bitmap createWaterMaskRightBottom( Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingBottom) { return createWaterMaskBitmap(src, watermark, src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight), src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom)); } /** * Set watermark image to the upper right corner * @param Context * @param src * @param watermark * @param paddingRight * @param paddingTop * @return */ public static Bitmap createWaterMaskRightTop( Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingTop) { return createWaterMaskBitmap( src, watermark, src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight), dp2px(context, paddingTop)); } /** * Set watermark image to the lower left corner * @param Context * @param src * @param watermark * @param paddingLeft * @param paddingBottom * @return */ public static Bitmap createWaterMaskLeftBottom( Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingBottom) { return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft), src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom)); } /** * Set watermark pictures to the center * @param Context * @param src * @param watermark * @return */ public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) { return createWaterMaskBitmap(src, watermark, (src.getWidth() - watermark.getWidth()) / 2, (src.getHeight() - watermark.getHeight()) / 2); } /** * Add text to the top left corner of the picture * @param context * @param bitmap * @param text * @return */ public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text, int size, int color, int paddingLeft, int paddingTop) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, dp2px(context, paddingLeft), dp2px(context, paddingTop) + bounds.height()); } /** * Draw text to the lower right corner * @param context * @param bitmap * @param text * @param size * @param color * @param paddingLeft * @param paddingTop * @return */ public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingBottom) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight), bitmap.getHeight() - dp2px(context, paddingBottom)); } /** * Draw text to the upper right * @param context * @param bitmap * @param text * @param size * @param color * @param paddingRight * @param paddingTop * @return */ public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingTop) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight), dp2px(context, paddingTop) + bounds.height()); } /** * Draw text to the bottom left * @param context * @param bitmap * @param text * @param size * @param color * @param paddingLeft * @param paddingBottom * @return */ public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingLeft, int paddingBottom) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, dp2px(context, paddingLeft), bitmap.getHeight() - dp2px(context, paddingBottom)); } /** * Draw text to the middle * @param context * @param bitmap * @param text * @param size * @param color * @return */ public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text, int size, int color) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); paint.setTextSize(dp2px(context, size)); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, (bitmap.getWidth() - bounds.width()) / 2, (bitmap.getHeight() + bounds.height()) / 2); } // Draw text on the picture private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text, Paint paint, Rect bounds, int paddingLeft, int paddingTop) { android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); paint.setDither(true); // Get samples with clear images paint.setFilterBitmap(true);// Filter some if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; } bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); canvas.drawText(text, paddingLeft, paddingTop, paint); return bitmap; } /** * Zoom pictures * @param src * @param w * @param h * @return */ public static Bitmap scaleWithWH(Bitmap src, double w, double h) { if (w == 0 || h == 0 || src == null) { return src; } else { // Record src Width of height int width = src.getWidth(); int height = src.getHeight(); // Create a matrix container Matrix matrix = new Matrix(); // Calculate scaling float scaleWidth = (float) (w / width); float scaleHeight = (float) (h / height); // Zoom start matrix.postScale(scaleWidth, scaleHeight); // Create a zoomed picture return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true); } } /** * dip turn pix * @param context * @param dp * @return */ public static int dp2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } }
Use as follows:
Add a layout, the above is the original picture, the following is to add the watermark after the picture
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/sour_pic_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Original " /> <ImageView android:id="@+id/sour_pic" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerInside"/> <TextView android:id="@+id/watermark_pic_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" watermark " /> <ImageView android:id="@+id/wartermark_pic" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerInside"/> </LinearLayout>
In the Activity to get to the ImageView object, and get Bitmap object, the Bitmap canva drawing, add watermark:
/** * Picture tool class * @author Cold water * */ public class MainActivity extends Activity { private ImageView mSourImage; private ImageView mWartermarkImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ mSourImage = (ImageView) findViewById(R.id.sour_pic); mWartermarkImage = (ImageView) findViewById(R.id.wartermark_pic); Bitmap sourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sour_pic); mSourImage.setImageBitmap(sourBitmap); Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.weixin); Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(sourBitmap, waterBitmap); watermarkBitmap = ImageUtil.createWaterMaskLeftBottom(this, watermarkBitmap, waterBitmap, 0, 0); watermarkBitmap = ImageUtil.createWaterMaskRightBottom(this, watermarkBitmap, waterBitmap, 0, 0); watermarkBitmap = ImageUtil.createWaterMaskLeftTop(this, watermarkBitmap, waterBitmap, 0, 0); watermarkBitmap = ImageUtil.createWaterMaskRightTop(this, watermarkBitmap, waterBitmap, 0, 0); Bitmap textBitmap = ImageUtil.drawTextToLeftTop(this, watermarkBitmap, " Top left corner ", 16, Color.RED, 0, 0); textBitmap = ImageUtil.drawTextToRightBottom(this, textBitmap, " Bottom right corner ", 16, Color.RED, 0, 0); textBitmap = ImageUtil.drawTextToRightTop(this, textBitmap, " Top right corner ", 16, Color.RED, 0, 0); textBitmap = ImageUtil.drawTextToLeftBottom(this, textBitmap, " Lower left quarter ", 16, Color.RED, 0, 0); textBitmap = ImageUtil.drawTextToCenter(this, textBitmap, " Middle ", 16, Color.RED); mWartermarkImage.setImageBitmap(textBitmap); } }
The above is the full content of this article, we hope to learn all the help, but also hope that we support a lot of script home.
if you want to reproduce, please indicate the source:
Android to the picture plus text and image watermarking example code - CodeDay