Add watermark image in android

I have code to add a watermark to an image like this

public static Bitmap mark(Bitmap src, String watermark, Point location, Color color, int alpha, int size, boolean underline) { int w = src.getWidth(); int h = src.getHeight(); Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); Canvas canvas = new Canvas(result); canvas.drawBitmap(src, 0, 0, null); Paint paint = new Paint(); paint.setColor(color.RED); paint.setAlpha(alpha); paint.setTextSize(size); paint.setAntiAlias(true); paint.setUnderlineText(underline); canvas.drawText(watermark, location.x, location.y, paint); return result; } 

and I call this function with this code

 mark(bitmap, "watermark", b, null, c, 100, false); imgshoot.setImageBitmap(bitmap); 

but nothing happens can help me? thanks

+4
source share
3 answers

he decided I just changed a little for this code and thanks for ur Doomsknight advice :)

  public static Bitmap mark(Bitmap src, String watermark) { int w = src.getWidth(); int h = src.getHeight(); Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); Canvas canvas = new Canvas(result); canvas.drawBitmap(src, 0, 0, null); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(18); paint.setAntiAlias(true); paint.setUnderlineText(true); canvas.drawText(watermark, 20, 25, paint); return result; } 

and I call this function

 bitmap = mark(bitmap, "Hallo"); imgshoot.setImageBitmap(bitmap); 
+8
source

You do not assign the returned bitmap result to anything. Replace the old bitmap with a new one.

  bitmap = mark(bitmap, "watermark", b, null, c, 100, false); imgshoot.setImageBitmap(bitmap); 

EDIT:

according to the comments, you still have problems: try hard-tuning some parameters to check. To find out if there are problems with your settings.

  Paint paint = new Paint(); paint.setColor(color.RED); //paint.setAlpha(alpha); paint.setTextSize(20); //size //paint.setAntiAlias(true); paint.setUnderlineText(underline); canvas.drawText(watermark, 10, 10, paint); //location.x, location.y 
+2
source
 private Bitmap addWaterMark(Bitmap src) { int w = src.getWidth(); int h = src.getHeight(); Bitmap result = Bitmap.createBitmap(w,h,src.getConfig()); Canvas canvas = new Canvas(result); canvas.drawBitmap(src,0,0, null); Bitmap waterMark = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_laucher); canvas.drawBitmap(waterMark,0,0,null); return result; } 
0
source

Source: https://habr.com/ru/post/1444542/


All Articles