3

I want to add water mark to image in android app. In my app user can select a image and write text in a edit text,after pressing a button i want selected image with watermark text on it. I have try below code but it is not working.

 public static void mark(String watermark,String filePath) {
        try{
                Bitmap bmp = BitmapFactory.decodeFile(filePath);
                int w = bmp.getWidth();
                int h = bmp.getHeight();
                Bitmap result = Bitmap.createBitmap(100, 100, bmp.getConfig());
                Canvas canvas = new Canvas(result);
                canvas.drawBitmap(bmp, 0, 0, null);
                Paint paint = new Paint();
                paint.setColor(Color.WHITE);
                paint.setAlpha(40);
                paint.setTextSize(20);
                paint.setAntiAlias(true);
                canvas.drawText(watermark,w/2, h/2+20, paint);
                File nFile = new File(getDevicePath(mContext)+"output1.jpg");
                nFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(nFile);
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                fOut.flush();
                fOut.close();   
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

please give me the way to solve above problem.

Thanks in advance.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68

3 Answers3

1

Issue 1: Don't you want the "watermarked image" to be the same size as the original image? So, instead of hard coded values:

           Bitmap result = Bitmap.createBitmap(w, h, bmp.getConfig());

Issue 2: Isn't it the result bitmap you want to write out? So, instead:

           result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

Issue 3: Is a file, even if the picture is not as you would like, even getting written where you want it? You may need to add a "/" separator using:

           File nFile = new File(getDevicePath(mContext)+ File.separator + "output1.jpg");
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
1

Please define this code into your method and create a dynamic textview for implementing this. This works fine for me and the imgPreviewRotate is my imageview so please change the imageview and check it out.

TextView txtWatermark = new TextView(MainActivity.this);
txtWatermark.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
txtWatermark.setText(getResources().getString("Dummy text"));
txtWatermark.setTextSize(5.0f);
txtWatermark.setPadding(10, 0, 30, 0);
txtWatermark.setTypeface(FontStyle.OswaldRegular(PreviewActivity.this));
txtWatermark.setTextColor(Color.parseColor("#FF0000"));
rlPreview.addView(imgPreviewRotate);
rlPreview.addView(txtWatermark);
txtWatermark.setVisibility(View.INVISIBLE);
Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35
0

it my fault.i found solution to my own problem.Thanks for your answers.i am using wrong bitmap.
in line -

 bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

i was using wrong bitmap,that's why it was not working.instead of i have to use

 result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

and after change above line it is work as a charm.

Hope it will help other.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68