如果你想这样做,那么实现下面的代码片段:

wHLoU.png

/** * PUT THIS METHOD FOR IMPLEMENT WATER-MARK IN COMMON FILE */ public static Bitmap waterMark(Bitmap src, String watermark) { //get source image width and height int w = src.getWidth(); int h = src.getHeight(); Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); Canvas canvas = new Canvas(result); canvas.drawBitmap(src, 0, 0, null); Paint paint = new Paint(); Paint.FontMetrics fm = new Paint.FontMetrics(); paint.setColor(Color.WHITE); paint.getFontMetrics(fm); int margin = 5; canvas.drawRect(50 - margin, 50 + fm.top - margin, 50 + paint.measureText(watermark) + margin, 50 + fm.bottom + margin, paint); paint.setColor(Color.RED); canvas.drawText(watermark, 50, 50, paint); return result; }

//从URI获取位图:

private Bitmap getBitmapFromUri(String photoPath) { Bitmap image = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options); return bitmap; }

// 保存图片 :

private String SaveImage(Bitmap finalBitmap) { String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/shareImage"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image" + n + ".jpg"; File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); finalBitmap.compress(Bitmap.CompressFormat.JPEG, 20, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return file.getAbsolutePath(); }

//像这样打电话:

Bitmap bitmap = getBitmapFromUri(attachment.get(i).getPath()); // Enter here your Image path Bitmap bitmapp = waterMark(bitmap, "ENTER YOUR TEXT FOR WATERMARK LABEL"); String path = SaveImage(bitmapp); Uri uri = Uri.fromFile(new File(path));

最后来自uri,您可以获得一个新的实现水印图像。

希望这对你有所帮助。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐