水印出现在整个图像Android上

Posted

技术标签:

【中文标题】水印出现在整个图像Android上【英文标题】:Watermark coming on whole image Android 【发布时间】:2016-08-11 03:29:33 【问题描述】:

我想把水印放在图片的右下角。但是,在某些手机中,它会完美呈现,而在某些手机中,水印会出现在整个图像上。

这是我的代码:

rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                            loadedImage.getWidth(), loadedImage.getHeight(),
                            rotateMatrix, false);

                    int w = rotatedBitmap.getWidth();
                    int h = rotatedBitmap.getHeight();
                    Bitmap result = Bitmap.createBitmap(w, h, rotatedBitmap.getConfig());
                    Canvas canvas = new Canvas(result);
                    canvas.drawBitmap(rotatedBitmap, 0, 0, null);

                    Bitmap waterMark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark2);
                int ww = waterMark.getWidth();
                int hh = waterMark.getHeight();
                    canvas.drawBitmap(waterMark,(w-ww),(h-hh), null);

编辑:这是结果的屏幕截图。在第二张图片中,水印完美出现,在第一张图片中,它出现在整个图片中。

【问题讨论】:

请截图 根据目标图像的大小缩放水印。 @PratikButani 我已经添加了截图 什么是wwwhhh @PratikButani w 和 h 是图像的宽度和高度, ww 和 hh 是水印的宽度和高度。可以在代码中看到 【参考方案1】:

利用纵横比。计算设备的纵横比,并据此计算适合水印的尺寸。

如何计算宽高比?

检查屏幕的宽度和高度,以较大者除以较小者 示例:

if(screen_width > screen_height) aspectRatio = screen_width/screen_height else aspectRatio = screen_height/screen_width

现在您已经计算出纵横比,将其乘以水印的大小并相应地进行缩放。

喜欢:

float ww = watermark.getWidth()*aspectRatio; float wh = watermark.getHeight()*aspectRatio;

并使用这些值。

希望这会有所帮助...

【讨论】:

出现同样的问题。【参考方案2】:

只需给出比例即可,尝试更改最适合您的 src 位图的比例值。

在源图像上嵌入图像水印以生成 水印的。

@param source 应该放置水印的源图片

@param ratio 一个浮点值

尝试将其从 0.20 更改为 0.60 以获得正确的结果

public static Bitmap addWatermark(Context context, Bitmap source, float ratio) 

 Canvas canvas;
 Paint paint;
 Bitmap bmp;
 Matrix matrix;
 RectF r;

 int width, height;
 float scale;

 Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.watermark3x);

 width = source.getWidth();
 height = source.getHeight();

 // Create the new bitmap
 bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
 paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);

 // Copy the original bitmap into the new one
 canvas = new Canvas(bmp);
 canvas.drawBitmap(source, 0, 0, paint);

 // Scale the watermark to be approximately to the ratio given of the source image height
 scale = (float) (((float) height * ratio) / (float) waterMark.getHeight());

 // Create the matrix
 matrix = new Matrix();
 matrix.postScale(scale, scale);

 // Determine the post-scaled size of the watermark
 r = new RectF(0, 0, waterMark.getWidth(), waterMark.getHeight());
 matrix.mapRect(r);

 // Move the watermark to the bottom right corner
 matrix.postTranslate(width - r.width(), height - r.height());

  // Move the watermark to the bottom left corner
  // matrix.postTranslate(0 , height - r.height());

  // Move the watermark to the top-left corner
  // matrix.postTranslate(0 ,0);

  // Move the watermark to the top right corner
  // matrix.postTranslate(width - r.width(), 0l̥);


 // Draw the watermark
 canvas.drawBitmap(waterMark, matrix, paint);

 return bmp;

【讨论】:

以上是关于水印出现在整个图像Android上的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何在 Android 中为图像添加水印效果? [复制]

在 react-native 中,我如何在 ios 和 android 的图像上添加水印

以编程方式在 Android 中为视频添加图像帧或水印

如何在 Android 中为图像添加水印效果?

android图像处理系列之七--图片涂鸦,水印-图片叠加

如何在 Agora android 中添加文字水印?