com.feingto.cloud.kit.ImageKit Maven / Gradle / Ivy
package com.feingto.cloud.kit;
import lombok.SneakyThrows;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
/**
* 图形处理工具
*
* @author longfei
*/
public class ImageKit {
private int redBins = 4;
private int greenBins = 4;
private int blueBins = 4;
@SneakyThrows
public static void cutCenterImage(String src, String dest, int w, int h) {
Iterator iterator = ImageIO.getImageReadersByFormatName(FileKit.getFileExt(src));
ImageReader reader = iterator.next();
try (InputStream in = new FileInputStream(src)) {
try (ImageInputStream iis = ImageIO.createImageInputStream(in)) {
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
int imageIndex = 0;
Rectangle rect = new Rectangle((reader.getWidth(imageIndex) - w) / 2, (reader.getHeight(imageIndex) - h) / 2, w, h);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, FileKit.getFileExt(src), new File(dest));
}
}
}
@SneakyThrows
public static void cutHalfImage(String src, String dest) {
Iterator iterator = ImageIO.getImageReadersByFormatName(FileKit.getFileExt(src));
ImageReader reader = iterator.next();
try (InputStream in = new FileInputStream(src)) {
try (ImageInputStream iis = ImageIO.createImageInputStream(in)) {
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
int imageIndex = 0;
int width = reader.getWidth(imageIndex) / 2;
int height = reader.getHeight(imageIndex) / 2;
Rectangle rect = new Rectangle(width / 2, height / 2, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, FileKit.getFileExt(src), new File(dest));
}
}
}
@SneakyThrows
public static void cutImage(String src, String dest, int x, int y, int w, int h) {
Iterator iterator = ImageIO.getImageReadersByFormatName(FileKit.getFileExt(src));
ImageReader reader = iterator.next();
try (InputStream in = new FileInputStream(src)) {
try (ImageInputStream iis = ImageIO.createImageInputStream(in)) {
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, w, h);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, FileKit.getFileExt(src), new File(dest));
}
}
}
@SneakyThrows
public static void zoomImage(String src, String dest, int w, int h) {
double wr;
double hr;
File srcFile = new File(src);
File destFile = new File(dest);
BufferedImage bufImg = ImageIO.read(srcFile);
bufImg.getScaledInstance(w, h, 4);
wr = (double) w * 1.0D / (double) bufImg.getWidth();
hr = (double) h * 1.0D / (double) bufImg.getHeight();
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
BufferedImage Itemp = ato.filter(bufImg, null);
ImageIO.write(Itemp, dest.substring(dest.lastIndexOf(".") + 1), destFile);
}
@SneakyThrows
public static void compressImage(String fileName) {
BufferedImage image = ImageIO.read(new File(fileName));
BufferedImage target = new BufferedImage(image.getWidth(), image.getHeight(), 8);
Graphics g = target.getGraphics();
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
ImageIO.write(target, "png", new File(fileName));
}
public double matchImage(String img1, String img2) {
try {
File desc = new File(img2);
if (!desc.exists()) {
return 0.0D;
} else {
return this.modelMatch(ImageIO.read(new File(img1)), ImageIO.read(desc));
}
} catch (IOException e) {
return 0.0D;
}
}
public void setRedBinCount(int redBinCount) {
this.redBins = redBinCount;
}
public void setGreenBinCount(int greenBinCount) {
this.greenBins = greenBinCount;
}
public void setBlueBinCount(int blueBinCount) {
this.blueBins = blueBinCount;
}
private float[] filter(BufferedImage src) {
int width = src.getWidth();
int height = src.getHeight();
int[] inPixels = new int[width * height];
float[] histogramData = new float[this.redBins * this.greenBins * this.blueBins];
this.getRGB(src, width, height, inPixels);
float total = 0.0F;
int row;
for (row = 0; row < height; ++row) {
for (int col = 0; col < width; ++col) {
int index = row * width + col;
int tr = inPixels[index] >> 16 & 255;
int tg = inPixels[index] >> 8 & 255;
int tb = inPixels[index] & 255;
int redIdx = (int) this.getBinIndex(this.redBins, tr);
int greenIdx = (int) this.getBinIndex(this.greenBins, tg);
int blueIdx = (int) this.getBinIndex(this.blueBins, tb);
int singleIndex = redIdx + greenIdx * this.redBins + blueIdx * this.redBins * this.greenBins;
++histogramData[singleIndex];
++total;
}
}
for (row = 0; row < histogramData.length; ++row) {
histogramData[row] /= total;
}
return histogramData;
}
private float getBinIndex(int binCount, int color) {
float binIndex = (float) color / (float) 255 * (float) binCount;
if (binIndex >= (float) binCount) {
binIndex = (float) (binCount - 1);
}
return binIndex;
}
private void getRGB(BufferedImage image, int width, int height, int[] pixels) {
int type = image.getType();
if (type != 2 && type != 1) {
image.getRGB(0, 0, width, height, pixels, 0, width);
} else {
image.getRaster().getDataElements(0, 0, width, height, pixels);
}
}
private double modelMatch(BufferedImage sourceImage, BufferedImage candidateImage) {
float[] sourceData = this.filter(sourceImage);
float[] candidateData = this.filter(candidateImage);
double[] mixedData = new double[sourceData.length];
double similarity = 0.0D;
for (int i = 0; i < sourceData.length; ++i) {
mixedData[i] = Math.sqrt((double) (sourceData[i] * candidateData[i]));
}
for (double aMixedData : mixedData) {
similarity += aMixedData;
}
return similarity;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy