https://www.cnblogs.com/Lixiaogang/p/13157343.html

应业务需求,需要对图片进行二次处理,裁剪出不规则图形保存,原图画不规则图形.

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;public class ImageUtils {  /*** 矩形裁剪jpg* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param x 原点x坐标* @param y 原点y坐标* @param width 宽* @param height 高* @throws IOException*/public static void cut_JPG(String inputFilePath, String outFilePath, int x,int y, int width, int height) throws IOException {ImageInputStream imageStream = null;try {FileInputStream input = new FileInputStream(inputFilePath);Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");ImageReader reader = readers.next();imageStream = ImageIO.createImageInputStream(input);reader.setInput(imageStream, true);ImageReadParam param = reader.getDefaultReadParam();Rectangle rect = new Rectangle(x, y, width, height);param.setSourceRegion(rect);BufferedImage bi = reader.read(0, param);FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(bi, "jpg", out);input.close();out.close();} finally {imageStream.close();}}/*** 矩形裁剪png* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param x 原点x坐标* @param y 原点y坐标* @param width 宽* @param height 高* @throws IOException*/public static void cut_PNG(String inputFilePath, String outFilePath, int x,int y, int width, int height) throws IOException {ImageInputStream imageStream = null;try {FileInputStream input = new FileInputStream(inputFilePath);Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("png");ImageReader reader = readers.next();imageStream = ImageIO.createImageInputStream(input);reader.setInput(imageStream, true);ImageReadParam param = reader.getDefaultReadParam();Rectangle rect = new Rectangle(x, y, width, height);param.setSourceRegion(rect);BufferedImage bi = reader.read(0, param);FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(bi, "png", out);input.close();out.close();} finally {imageStream.close();}}/*** 矩形裁剪* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param type 图片类型* @param x 原点x坐标* @param y 原点y坐标* @param width 宽* @param height 高* @throws IOException*/public static void cut_Image(String inputFilePath, String outFilePath, String type,int x,int y, int width, int height) throws IOException {ImageInputStream imageStream = null;try {FileInputStream input = new FileInputStream(inputFilePath);String imageType=(null==type||"".equals(type))?"jpg":type;Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageType);ImageReader reader = readers.next();imageStream = ImageIO.createImageInputStream(input);reader.setInput(imageStream, true);ImageReadParam param = reader.getDefaultReadParam();Rectangle rect = new Rectangle(x, y, width, height);param.setSourceRegion(rect);BufferedImage bi = reader.read(0, param);FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(bi, imageType, out);input.close();out.close();} finally {imageStream.close();}}/*** 图片矩形裁切* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param x 原点x坐标* @param y 原点y坐标* @param width 宽* @param height 高*/public static void cut_Image2(String inputFilePath,String outFilePath,int x, int y, int width, int height) {try {File f = new File(inputFilePath);File t = new File(outFilePath);if (t.exists()) {t.delete();}//图片输入流ImageInputStream input = ImageIO.createImageInputStream(f);//图片读取器Iterator<ImageReader> it = ImageIO.getImageReaders(input);if (it.hasNext()) {ImageReader r = it.next();//设置输入流r.setInput(input, true);//读取参数ImageReadParam param = r.getDefaultReadParam();//创建要截取的矩形范围Rectangle rect = new Rectangle(x, y, width, height);//设置截取范围参数param.setSourceRegion(rect);//读取截图数据BufferedImage bi = r.read(0, param);// 保存图片ImageIO.write(bi, "jpg", t);}input.close();} catch (Exception e) {e.printStackTrace();}}/*** 裁剪(多边形)* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param x[] x轴坐标点数组* @param y[] y轴坐标点数组* @param n 坐标点数量* @throws IOException*/public static void cutPolygon_Image( String inputFilePath,String outFilePath,int x[],  int y[],int n) throws IOException {try {BufferedImage image = ImageIO.read(new File(inputFilePath));GeneralPath clip = new GeneralPath(GeneralPath.WIND_EVEN_ODD, n);//int x[]={860,650,300,1700};//int y[]={20,20,1000,1000};clip.moveTo(x[0], y[0]);for (int i = 1; i < x.length; i++) {clip.lineTo(x[i], y[i]);}clip.closePath();Rectangle bounds = clip.getBounds();//BufferedImage img = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_BGR);BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_BGR);Graphics2D g2d = img.createGraphics();//clip.transform(AffineTransform.getTranslateInstance(0, 0));g2d.setClip(clip);//g2d.translate(0, 0);g2d.drawImage(image, 0, 0, null);g2d.dispose();FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(img, "jpg", out);out.close();} catch (Exception e) {e.printStackTrace();}}/*** 绘图(图片上添加文字)* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param list 文字内容及坐标* @throws IOException*/public static void drawingText_Image( String inputFilePath,String outFilePath,List<Map<String,Object>> list) throws IOException {try {/*List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();Map<String, Object> map = null;map = new HashMap<String , Object>();map.put("text","文字内容");map.put("x", 100);map.put("y", 100);listMap.add(map);*/BufferedImage image = ImageIO.read(new File(inputFilePath));Graphics2D g =(Graphics2D) image.getGraphics();g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);g.setColor(Color.YELLOW); //字体颜色g.setFont(new Font("微软雅黑", Font.BOLD, 32)); //字体样式for (Map<String, Object> map : list) {g.drawString((String) map.get("text"), (int)map.get("x"),(int)map.get("y"));}g.dispose();FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(image, "jpg", out);out.close();} catch (Exception e) {e.printStackTrace();}}/*** 绘图(画矩形框)* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param x 原点x坐标* @param y 原点y坐标* @param width 宽* @param height 高*/public static void drawingRectangular_Image( String inputFilePath,String outFilePath, int x,  int y, int width, int height) throws IOException {try {BufferedImage image = ImageIO.read(new File(inputFilePath));Graphics2D g =(Graphics2D) image.getGraphics();g.setColor(Color.YELLOW);//画笔颜色g.setStroke(new BasicStroke(3.0f));//设置线宽g.drawRect(x, y, width, height);//矩形框(原点x坐标,原点y坐标,矩形的长,矩形的宽)//g.fillRect(x, y, width, height);//画矩形着色块//g.draw3DRect(x, y, width, height,true);//画三维矩形g.dispose();FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(image, "jpg", out);out.close();} catch (Exception e) {e.printStackTrace();}} /*** 绘图(画多边形框)* @param inputFilePath 图片输入路径* @param outFilePath 图片输出路径* @param x[] x轴坐标点数组* @param y[] y轴坐标点数组* @param n 坐标点数量* @throws IOException*/public static void drawingPolygon_Image( String inputFilePath,String outFilePath, int x[],  int y[],int n) throws IOException {try {BufferedImage image = ImageIO.read(new File(inputFilePath));Graphics2D g =(Graphics2D) image.getGraphics();g.setColor(Color.YELLOW);//画笔颜色g.setStroke(new BasicStroke(3.0f));//设置线宽//int x[]={860,650,300,1700};//int y[]={20,20,1000,1000};g.drawPolygon(x,y,n);// 画多边形g.dispose();FileOutputStream out = new FileOutputStream(outFilePath);//输出图片的地址ImageIO.write(image, "jpg", out);out.close();} catch (Exception e) {e.printStackTrace();}} /**  * 添加图片水印 * @param iconPath 水印图片路径* @param inputFilePath 源图片路径  * @param outFilePath 图片输出路径  * @param degree 水印图片旋转角度  */  public static void imageWatermarking_Image(String iconPath, String inputFilePath,String outFilePath, Integer degree) {   try {   Image srcImg = ImageIO.read(new File(inputFilePath));BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);// 得到画笔对象   // Graphics g= buffImg.getGraphics();   Graphics2D g = buffImg.createGraphics();   // 设置对线段的锯齿状边缘处理   g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);   g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg   .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);   if (degree != null) {// 设置水印旋转 g.rotate(Math.toRadians(degree),(double) buffImg.getWidth() / 2,(double) buffImg.getHeight() / 2);}// 水印图象的路径 水印一般为gif或者png的,这样可设置透明度   ImageIcon imgIcon = new ImageIcon(iconPath);// 得到Image对象。Image img = imgIcon.getImage();float alpha = 0.4f; // 透明度 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));// 表示水印图片的位置 g.drawImage(img, 15, 30, null);g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));g.dispose();OutputStream out = new FileOutputStream(outFilePath);// 生成图片ImageIO.write(buffImg, "jpg", out);out.close();} catch (Exception e) {e.printStackTrace();}}/*** 添加文字水印* @param inputFilePath 源图片路径* @param outFilePath 保存的图片路径* @param waterMarkContent 水印内容* @param degree 旋转角度* @param xmove 水印间距* @param ymove 水印间距*/public static void textWatermarking_Image(String inputFilePath,String outFilePath,String waterMarkContent,Integer degree , int xmove,int ymove) {try {// 读取原图片信息File srcImgFile = new File(inputFilePath);// 得到文件Image srcImg = ImageIO.read(srcImgFile);// 文件转化为图片int srcImgWidth = srcImg.getWidth(null);// 获取图片的宽int srcImgHeight = srcImg.getHeight(null);// 获取图片的高// 加水印BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight,BufferedImage.TYPE_INT_RGB);Graphics2D g = bufImg.createGraphics();// 设置对线段的锯齿状边缘处理g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);// g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);// 设置水印旋转if (null != degree) {g.rotate(Math.toRadians(degree),(double) bufImg.getWidth() / 2,(double) bufImg.getHeight() / 2);}Font font = new Font("宋体", Font.PLAIN, 20);g.setColor(new Color(107, 109, 106)); //水印颜色g.setFont(font); // 设置字体
//             g.setFont(new Font("宋体", Font.PLAIN, srcImg.getWidth(null)/300*15));// 设置水印文字透明度
//             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.5f));// 设置水印的坐标int x = -srcImgWidth / 2;int y = -srcImgHeight / 2;int markWidth = font.getSize() * getTextLength(waterMarkContent);// 字体长度int markHeight = font.getSize();// 字体高度
//             BigDecimal number= new BigDecimal(srcImg.getWidth(null));
//             BigDecimal olDnumber= new BigDecimal(800);//             Integer MOVE = number.divide(olDnumber).intValue()*150;// 循环添加水印while (x < srcImgWidth * 1.5) {y = -srcImgHeight / 2;while (y < srcImgHeight * 1.5) {g.drawString(waterMarkContent, x, y);y += markHeight + ymove;}x += markWidth + xmove;}g.dispose();// 输出图片FileOutputStream outImgStream = new FileOutputStream(outFilePath);ImageIO.write(bufImg, "jpg", outImgStream);outImgStream.flush();outImgStream.close();} catch (Exception e) {e.printStackTrace();}}/*** 获取文本长度。汉字为1:1,英文和数字为2:1*/private static int getTextLength(String text) {int length = text.length();for (int i = 0; i < text.length(); i++) {String s = String.valueOf(text.charAt(i));if (s.getBytes().length > 1) {length++;}}length = length % 2 == 0 ? length / 2 : length / 2 + 1;return length;}public static void main(String[] args) {String inputFilePath = "E:/picOcr/original/20200615000000001.jpg";String outFilePath = "E:/picOcr/interception/20200615000000001.jpg";String iconPath = "E:/picOcr/2020592210413248.jpg";try {//裁剪矩形//ImageUtils.cut_JPG(inputFilePath, outFilePath, 400,300,900,750);//ImageUtils.cut_Image(inputFilePath, outFilePath,"jpg", 400,300,900,750);//ImageUtils.cut_Image2(inputFilePath, outFilePath, 400,300,900,750);//裁剪多边形/*int x[]={860,650,300,1700};int y[]={20,20,1000,1000};ImageUtils.cutPolygon_Image(inputFilePath, outFilePath,x,y,4);*///绘图多边形/*int x[]={860,650,400,300,1700};int y[]={20,20,500,1000,1000};ImageUtils.drawingPolygon_Image(inputFilePath, outFilePath, x, y, 5);*///绘图矩形//ImageUtils.drawingRectangular_Image(inputFilePath, outFilePath, 400, 300, 900,750);//绘图 添加文字/*List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();Map<String, Object> map = null;map = new HashMap<String , Object>();map.put("text","文字内容");map.put("x", 100);map.put("y", 100);listMap.add(map);map = new HashMap<String , Object>();map.put("text","图片注释");map.put("x", 100);map.put("y", 140);listMap.add(map);ImageUtils.drawingText_Image(inputFilePath, outFilePath, listMap);*///添加图片水印//ImageUtils.imageWatermarking_Image(iconPath, inputFilePath, outFilePath, -45);//添加文字水印ImageUtils.textWatermarking_Image(inputFilePath, outFilePath, "版权所有", -45, 80, 80);} catch (Exception e) {e.printStackTrace();}}}

【转载】java 实现 图片不规则(多边形)裁剪,绘图,添加水印相关推荐

  1. Java 实现图片压缩、裁剪

    1.引入依赖 <!--hutool 工具--> <dependency><groupId>cn.hutool</groupId><artifact ...

  2. Java实现图片裁剪预览功能

    Java实现图片裁剪预览功能 在项目中,我们需要做些类似头像上传,图片裁剪的功能,ok看下面文章! 需要插件:jQuery Jcrop 后端代码: package org.csg.upload;imp ...

  3. Java 通过BufferedImage缩放和裁剪图片

    通过BufferedImage对图片进行缩放和裁剪 读取图片 //读取图片 通过JAVA自带的ImageIO里面的read方法 BufferedImage bufImage = ImageIO.rea ...

  4. java实现图片裁剪

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java实现图片裁剪,输入起点x.y,宽度width.高度height 测试代码 public static void main(String[] ...

  5. Android图片不规则裁剪

    demo链接: Android自定义不规则裁剪 扩展链接: Android:谈谈最被误读的属性adjustViewBounds 定义:1.adjustViewBounds影响的是ImageView的比 ...

  6. java实现图片平铺倾斜水印效果--转载

    转载地址--java实现图片平铺倾斜水印效果_java_clh的博客-CSDN博客_java图片平铺 效果图: public static void main(String[] args) throw ...

  7. Java使用thumbnailator进行图片压缩缩放裁剪水印旋转处理

    Java使用thumbnailator进行图片压缩缩放裁剪水印旋转处理 一.thumbnailator介绍 二.使用步骤 1.maven的pom.xml引入如下 2.测试代码 一.thumbnaila ...

  8. java实现图片固定长宽的缩放和裁剪

    java实现图片固定长宽的缩放和裁剪 实现对aa文件夹进行深层遍历并将图片按长600宽400进行缩放和裁剪 import java.awt.Color; import java.awt.Graphic ...

  9. wpf 图片不规则裁剪_张天爱到底有多美?不规则抹胸上衣搭配牛仔裤时髦精致,优雅迷人...

    不规则裁剪的单品设计感比较强烈,容易让人感觉到眼前一亮,很直观地显现出了搭配的特色.在出席正式场合时,穿得与众不同一点,能够迅速让自己成为全场关注的焦点,比如张天爱上身穿了一件抹胸的不规则上衣,中间短 ...

最新文章

  1. OCI读取单条记录(C)
  2. Javascript获取select的选中值和选中文本
  3. matlab vco,MATLAB锁相环仿真程序求解
  4. python 获得两个数组(List)交集、并集与差集
  5. JVM_06 垃圾回收相关算法 [ 一 ]
  6. 操作符(++,+,+=,小于号,(),--等)重载
  7. 一、linux搭建jenkins+github详细步骤
  8. 图表框架HelloCharts(3)饼状图
  9. 使用vue-cli初始化项目时运行‘npm run dev’报错
  10. 关于神经网络的调参经验技巧和调参顺序
  11. Pytorch 的迁移学习的理解
  12. Excel小技巧,隔行变色,多行变色
  13. strut2 上传文件
  14. win2003服务器性能工具,WindowsServer 2003 Service Pack 1 支持工具
  15. XP默认输入法快捷键修复
  16. 清晰扫描件怎么弄:试试扫描裁缝ScanTailor Advanced吧 | 含scantailor使用方法
  17. DTI预处理和脑网络构建
  18. 小米平板3最简单刷成开发版获得ROOT权限的步骤
  19. 前三周学习Python的心得与感受
  20. 高通平台蓝牙--安全issue

热门文章

  1. jsplumb拖线_jsPlumb系列问题:请问jsPlumb渲染出来之后线条没有连接节点?需要拖动之后才连上...
  2. 2016杭州云栖大会10月开幕 规模翻倍
  3. 2021-4-9天梯赛模拟赛3补题L1-6 吃火锅 (15 分)
  4. 蓝牙音频传输协议简介
  5. inspect python_python的inspect模块
  6. 普通心理学-学习笔记
  7. python小项目--》英汉互译词典
  8. python做上位机读usb数据_使用python实现win10系统和arduino usb串口通信
  9. 什么是OOM?如何解决OOM问题!
  10. disabled 属性