java图片加水印文字



public static final int FONT_SIZE = 18; //文字大小
 public static final String APP_NAME="来自app养花社区";  //需要加的文字
 /**
  *
  * @param targetImg 图片路径
  */
 public static void pressText(String targetImg) {
  try {
   File _file = new File(targetImg);
   Image src = ImageIO.read(_file);
   int wideth = src.getWidth(null);
   int height = src.getHeight(null);
   BufferedImage image = new BufferedImage(wideth, height,
     BufferedImage.TYPE_INT_RGB);
   Graphics g = image.createGraphics();
   g.drawImage(src, 0, 0, wideth, height, null);
   g.setColor(Color.black); //文字颜色
   g.setFont(new Font(APP_NAME, Font.PLAIN, FONT_SIZE));
   g.drawString(Contans.APP_NAME,//可以设置文字的位置
     wideth / 2 - APP_NAME.length(), height - 10);
   g.dispose();
   FileOutputStream out = new FileOutputStream(targetImg);
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   encoder.encode(image);
   out.close();
  } catch (Exception e) {
   System.out.println(e);
  }
  System.out.println("完成");
 }




压缩图片:

try {
   File fi = new File(imgaePath); // 大图文件
   System.out.print("ddddd: "+imgaePath.substring(0, imgaePath.indexOf(".")));
   File fo = new File("C:\\Users\\Huoyunren\\git\\yanghua\\MyYangHua\\WebRoot\\tiezi_images\\545.png"); // 将要转换出的小图文件
   int nw = 500;
   /*
    * AffineTransform 类表示 2D 仿射变换,它执行从 2D 坐标到其他 2D
    * 坐标的线性映射,保留了线的“直线性”和“平行性”。可以使用一系 列平移、缩放、翻转、旋转和剪切来构造仿射变换。
    */
   AffineTransform transform = new AffineTransform();
   BufferedImage bis = ImageIO.read(fi); // 读取图片
   int w = bis.getWidth();
   int h = bis.getHeight();
   // double scale = (double)w/h;
   int nh = (nw * h) / w;
   double sx = (double) nw / w;
   double sy = (double) nh / h;
   transform.setToScale(sx, sy); // setToScale(double sx, double sy)
           // 将此变换设置为缩放变换。
   /*
    * AffineTransformOp类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或 Raster 中 2D
    * 坐标的线性映射。所使用的插值类型由构造方法通过 一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
    * 如果在构造方法中指定了 RenderingHints 对象,则使用插值提示和呈现
    * 的质量提示为此操作设置插值类型。要求进行颜色转换时,可以使用颜色 呈现提示和抖动提示。 注意,务必要满足以下约束:源图像与目标图像
    * 必须不同。 对于 Raster 对象,源图像中的 band 数必须等于目标图像中 的 band 数。
    */
   AffineTransformOp ato = new AffineTransformOp(transform, null);
   BufferedImage bid = new BufferedImage(nw, nh,
     BufferedImage.TYPE_3BYTE_BGR);
   /*
    * TYPE_3BYTE_BGR 表示一个具有 8 位 RGB 颜色分量的图像, 对应于 Windows 风格的 BGR
    * 颜色模型,具有用 3 字节存 储的 Blue、Green 和 Red 三种颜色。
    */
   ato.filter(bis, bid);
   ImageIO.write(bid, "jpeg", fo);
  } catch (Exception e) {
   e.printStackTrace();
  System.out.print("eeee: "+e.toString());
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值