gpt4 book ai didi

android - 在android中为图像添加水印

转载 作者:太空狗 更新时间:2023-10-29 13:21:51 24 4
gpt4 key购买 nike

我想在 android 应用程序中为图像添加水印。在我的应用程序中,用户可以选择一个图像并在编辑文本中写入文本,按下按钮后我想要选择带有水印文本的图像。我尝试了以下代码,但它不起作用。

 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();
}
}

请给我解决上述问题的方法。

提前致谢。

最佳答案

问题一:你不想让“水印图”和原图一样大吗?因此,不是硬编码值:

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

问题2:你要写出来的不是result位图吗?所以,相反:

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

问题三:是不是一个文件,即使图片不是你想要的,也能写到你想要的地方?您可能需要使用以下方法添加“/”分隔符:

           File nFile = new File(getDevicePath(mContext)+ File.separator + "output1.jpg");

关于android - 在android中为图像添加水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com