Add water sign for image in android
I want to add a water sign to an image in an android app. In my application, user can select an image and write text in edit text, after clicking a button, I want the selected image with watermark text on it. I am trying to make the code below but it doesn't work.
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 a way to solve the above problem.
Thanks in advance.
source to share
this is my mistake. I found a solution to my own problem. Thank you for your responses. I am using the wrong bitmap.
in line -
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
I used the wrong bitmap, so it didn't work. Instead, I have to use
result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
and after changing the above line it works like a charm.
Hope it helps others.
source to share
Problem 1: You don't want the watermarked image to be the same size as the original image? So instead of hardcoded values:
Bitmap result = Bitmap.createBitmap(w, h, bmp.getConfig());
Problem 2: Isn't this the bitmap result
you want to burn? So instead of this:
result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
Problem 3: Is the file even if the image is not the way you would like, even writing where you want? You may need to add a "/" separator using:
File nFile = new File(getDevicePath(mContext)+ File.separator + "output1.jpg");
source to share
Please define this code in your method and create a dynamic text view to implement it. This works great for me and imgPreviewRotate is my image, so please change the image and check it.
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);
source to share