Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中为图像添加水印_Android_Image Processing_Canvas_Watermark - Fatal编程技术网

在android中为图像添加水印

在android中为图像添加水印,android,image-processing,canvas,watermark,Android,Image Processing,Canvas,Watermark,我想在android应用程序中为图像添加水印。 在我的应用程序中,用户可以选择图像并在编辑文本中写入文本,按下按钮后,我希望选择的图像上有水印文本。 我尝试了下面的代码,但它不工作 public static void mark(String watermark,String filePath) { try{ Bitmap bmp = BitmapFactory.decodeFile(filePath); int

我想在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();
            }
        }
请告诉我解决上述问题的方法


提前感谢。

第1期:您不希望“带水印的图像”与原始图像大小相同吗?因此,与硬编码值不同:

           Bitmap result = Bitmap.createBitmap(w, h, bmp.getConfig());
问题2:这不是您要写出的
结果
位图吗?因此,取而代之的是:

           result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
问题3:是一个文件,即使图片不是你想要的,甚至可以写在你想要的地方?您可能需要使用以下方式添加“/”分隔符:

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

请在方法中定义此代码,并创建一个动态textview来实现此功能。这对我来说很好,imgPreviewRotate是我的imageview,所以请更改imageview并查看它

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

这是我的错。我找到了解决我自己问题的方法。谢谢你的回答。我使用了错误的位图。
符合-

 bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
我使用了错误的位图,这就是为什么它不起作用的原因

 result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
而经过线以上的改变后,它就成了一种魅力


希望它能帮助其他人。

它在哪些方面“不起作用”?我认为它得到了任何解决方案没有得到带有水印的输出图像。这是我的错。我找到了解决我自己问题的方法。谢谢你的回答。我使用了错误的位图。谢谢你的回答。+1谢谢你的回答。