When we are working on projects, we sometimes need to add watermarks to the images. Shuihan encountered such a problem today, so we created a tool class and posted it and called it directly.
/** * Picture tool category * @author Shuihan * */ public class ImageUtil { /** * Set the 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 = (); int height = (); //Create a bitmap Bitmap newb = (width, height, Config.ARGB_8888);// Create a new bitmap with the same length and width as the SRC //Please this picture as a canvas Canvas canvas = new Canvas(newb); //Start drawing original picture on the canvas 0, 0 coordinates (src, 0, 0, null); //Draw watermark pictures on the canvas (watermark, paddingLeft, paddingTop, null); // Save (Canvas.ALL_SAVE_FLAG); // Storage (); return newb; } /** * Set the watermark image in the lower 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, () - () - dp2px(context, paddingRight), () - () - dp2px(context, paddingBottom)); } /** * Set the 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, () - () - dp2px(context, paddingRight), dp2px(context, paddingTop)); } /** * Set the 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), () - () - dp2px(context, paddingBottom)); } /** * Set the watermark picture to the middle * @param Context * @param src * @param watermark * @return */ public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) { return createWaterMaskBitmap(src, watermark, (() - ()) / 2, (() - ()) / 2); } /** * Add text to the image to the upper left corner * @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); (color); (dp2px(context, size)); Rect bounds = new Rect(); (text, 0, (), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, dp2px(context, paddingLeft), dp2px(context, paddingTop) + ()); } /** * 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); (color); (dp2px(context, size)); Rect bounds = new Rect(); (text, 0, (), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, () - () - dp2px(context, paddingRight), () - 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); (color); (dp2px(context, size)); Rect bounds = new Rect(); (text, 0, (), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, () - () - dp2px(context, paddingRight), dp2px(context, paddingTop) + ()); } /** * 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); (color); (dp2px(context, size)); Rect bounds = new Rect(); (text, 0, (), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, dp2px(context, paddingLeft), () - 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); (color); (dp2px(context, size)); Rect bounds = new Rect(); (text, 0, (), bounds); return drawTextToBitmap(context, bitmap, text, paint, bounds, (() - ()) / 2, (() + ()) / 2); } //Draw text on the picture private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text, Paint paint, Rect bounds, int paddingLeft, int paddingTop) { bitmapConfig = (); (true); // Get clear image sampling (true);// Filter some if (bitmapConfig == null) { bitmapConfig = .ARGB_8888; } bitmap = (bitmapConfig, true); Canvas canvas = new Canvas(bitmap); (text, paddingLeft, paddingTop, paint); return bitmap; } /** * Zoom picture * @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 the width and height of src int width = (); int height = (); // Create a matrix container Matrix matrix = new Matrix(); // Calculate the scaling ratio float scaleWidth = (float) (w / width); float scaleHeight = (float) (h / height); // Start scaling (scaleWidth, scaleHeight); // Create a zoomed picture return (src, 0, 0, width, height, matrix, true); } } /** * Dip to Pix * @param context * @param dp * @return */ public static int dp2px(Context context, float dp) { final float scale = ().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); } }
How to use it is as follows:
Add a layout, the above is the original image, the following is the image after adding a watermark
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Original Picture" /> <ImageView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerInside"/> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Watermark" /> <ImageView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerInside"/> </LinearLayout>
Get the ImageView object in the Activity, and get the Bitmap object, perform canva drawing of Bitmap, and add a watermark:
/** * Picture tool category * @author Shuihan * */ public class MainActivity extends Activity { private ImageView mSourImage; private ImageView mWartermarkImage; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initView(); } private void initView(){ mSourImage = (ImageView) findViewById(.sour_pic); mWartermarkImage = (ImageView) findViewById(.wartermark_pic); Bitmap sourBitmap = (getResources(), .sour_pic); (sourBitmap); Bitmap waterBitmap = (getResources(), ); Bitmap watermarkBitmap = (sourBitmap, waterBitmap); watermarkBitmap = (this, watermarkBitmap, waterBitmap, 0, 0); watermarkBitmap = (this, watermarkBitmap, waterBitmap, 0, 0); watermarkBitmap = (this, watermarkBitmap, waterBitmap, 0, 0); watermarkBitmap = (this, watermarkBitmap, waterBitmap, 0, 0); Bitmap textBitmap = (this, watermarkBitmap, "Upper left corner", 16, , 0, 0); textBitmap = (this, textBitmap, "Lower Right", 16, , 0, 0); textBitmap = (this, textBitmap, "Upper Right", 16, , 0, 0); textBitmap = (this, textBitmap, "Lower left corner", 16, , 0, 0); textBitmap = (this, textBitmap, "middle", 16, ); (textBitmap); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.