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 {
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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);
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(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = img.createGraphics();
g2d.setClip(clip);
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();
}
}
public static void drawingText_Image( String inputFilePath,String outFilePath,List<Map<String,Object>> list) throws IOException {
try {
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();
}
}
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);
g.dispose();
FileOutputStream out = new FileOutputStream(outFilePath);
ImageIO.write(image, "jpg", out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
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));
g.drawPolygon(x,y,n);
g.dispose();
FileOutputStream out = new FileOutputStream(outFilePath);
ImageIO.write(image, "jpg", out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
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);
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);
}
ImageIcon imgIcon = new ImageIcon(iconPath);
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();
}
}
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.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);
int x = -srcImgWidth / 2;
int y = -srcImgHeight / 2;
int markWidth = font.getSize() * getTextLength(waterMarkContent);
int markHeight = font.getSize();
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();
}
}
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.textWatermarking_Image(inputFilePath, outFilePath, "版权所有", -45, 80, 80);
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文地址
https://www.cnblogs.com/Lixiaogang/p/13157343.html
本文链接:
https://jianz.xyz/index.php/archives/197/