import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import javax.imageio.ImageIO;
/**
*/
public class ImgUtil {
/**
* 判断图片类型
*
* @param ins
* @return
* @throws IOException
*/
public static String getImgTypeByStream(InputStream ins) throws IOException {
byte[] b = new byte[4];
ins.read(b, 0, b.length);
String type = IOUtil.byteToHex(b).toUpperCase();
if (type.contains("FFD8FF")) {
return "jpg";
} else if (type.contains("89504E47")) {
return "png";
} else if (type.contains("47494638")) {
return "gif";
} else if (type.contains("424D")) {
return "bmp";
} else {
return "unknown";
}
}
/**
* 等比例缩放图片
*
* @param stream
* @param scale
* 大于1放大、小于1缩小、等于1降质
* @return
* @throws IOException
*/
public static InputStream scaleImg(InputStream stream, float scale, String type) throws IOException {
BufferedImage inputImg = ImageIO.read(stream);
int width = (int) (inputImg.getWidth() * scale);
int height = (int) (inputImg.getHeight() * scale);
//如不设置PNG转JPG背景发红
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = outputImage.getGraphics();
Image image = inputImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(outputImage, type, out);
ByteArrayInputStream result = new ByteArrayInputStream(out.toByteArray());
out.close();
return result;
}
/**
* 生成指定宽高的图片
*
* @param stream
* @param width
* @param height
* @param auto
* 保持原图片长宽比例
* @param type
* @return
* @throws IOException
*/
public static InputStream generatorImgByWidthHeight(InputStream stream, int setWidth, int setHeight, boolean auto,
String type) throws IOException {
BufferedImage inputImg = ImageIO.read(stream);
if (auto) {
ArrayList paramsArrayList = getAutoWidthAndHeight(inputImg, setWidth, setHeight);
setWidth = paramsArrayList.get(0);
setHeight = paramsArrayList.get(1);
}
BufferedImage outputImage = new BufferedImage(setWidth, setHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = outputImage.getGraphics();
Image image = inputImg.getScaledInstance(setWidth, setHeight, Image.SCALE_DEFAULT);
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(outputImage, type, out);
ByteArrayInputStream result = new ByteArrayInputStream(out.toByteArray());
out.close();
return result;
}
/**
* 添加全局水印
* @param stream
* @param content 水印内容
* @param font 水印字体--> 幼圆
* @param color 字体颜色 -->255,255,240
* @param degree 旋转 --> -40
* @return
* @throws IOException
*/
public static InputStream imgWatermark(InputStream stream,String content,Font font,Color color,Integer degree,String type) throws IOException{
BufferedImage inputImg = ImageIO.read(stream);
Image image = inputImg.getScaledInstance(inputImg.getWidth(), inputImg.getHeight(), Image.SCALE_DEFAULT);
int srcImgWidth = image.getWidth(null);
int srcImgHeight = image.getHeight(null);
BufferedImage outputImage = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = outputImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image.getScaledInstance(image.getWidth(null),image.getHeight(null), Image.SCALE_SMOOTH),0,0,null);
if(degree!=null){
g.rotate(Math.toRadians(degree),(double)inputImg.getWidth() / 2,(double)inputImg.getHeight() / 2);
}
g.setColor(color);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
int x = -srcImgWidth / 2;
int y = -srcImgHeight / 2;
int markWidth = font.getSize() * getTextLength(content);
int markHeight = font.getSize();
while (x < srcImgWidth * 1.5) {
y = -srcImgHeight / 2;
while (y < srcImgHeight * 1.5) {
g.drawString(content, x, y);
y += markHeight + 100;
}
x += markWidth + 100;
}
g.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(outputImage, type, out);
ByteArrayInputStream result = new ByteArrayInputStream(out.toByteArray());
out.close();
return result;
}
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;
}
private static ArrayList getAutoWidthAndHeight(BufferedImage inputImg, int setWidth, int setHeight) {
ArrayList arrayList = new ArrayList();
int width = inputImg.getWidth();
int height = inputImg.getHeight();
double w = getDot2Decimal(setWidth, width);
double h = getDot2Decimal(setHeight, height);
if (w < h) {
arrayList.add((int) (w * width));
arrayList.add((int) (w * height));
} else {
arrayList.add((int) (h * width));
arrayList.add((int) (h * height));
}
return arrayList;
}
private static double getDot2Decimal(int a, int b) {
BigDecimal bigDecimal_1 = new BigDecimal(a);
BigDecimal bigDecimal_2 = new BigDecimal(b);
BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2, new MathContext(4));
Double double1 = new Double(bigDecimal_result.toString());
return double1;
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\ddd\\微信图片_20191029173946.jpg");
InputStream input = new FileInputStream(file);
// 大于1.5M在进行压缩
InputStream result = null;
if (input.available() > 1500000) {
result = scaleImg(input, 1f, "jpg");
} else {
result = input;
}
// Font font = new Font(“幼圆”, Font.PLAIN, 40);
// Color color = new Color(255,255,240);
// result = imgWatermark(input,“干就完了”,font,color,-40,“jpg”);
FileOutputStream out = new FileOutputStream(new File("D:\\ddd\\4c.jpg"));
out.write(IOUtil.toByteArray(result));
out.close();
result.close();
input.close();
}
}