import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class testImage
{
/**
* 图片旋转
* @param src 原始图
* @param angel 旋转角度
* @return 结果图字节数据组
*/
public static BufferedImage Rotate(Image src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
src_width, src_height)), angel);
BufferedImage res = null;
res = new BufferedImage(rect_des.width, rect_des.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
return res;
}
public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
if (angel >= 90) {
if(angel / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
/**
* 对图片中的 黑色或白色进行透明化处理
* @param sourcePath 原始图
* @param targetPath 目标图,为null时在原始图同级目录下生成目标图
* @return 结果图字节数据组
*/
public static byte[] transferAlpha(String sourcePath,String targetPath,int color) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
File iFile = new File(sourcePath);
if(!iFile.exists()) return byteArrayOutputStream.toByteArray();
ImageIcon imageIcon = new ImageIcon(ImageIO.read(iFile));
BufferedImage bufferedImage = new BufferedImage(
imageIcon.getIconWidth(), imageIcon.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
g2D.drawImage(imageIcon.getImage(), 0, 0,imageIcon.getImageObserver());
int alpha = 0;
for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);
if (checkColor(rgb,16,color)) {
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
File targetFile = null;
if(targetPath == null){
targetFile = new File(sourcePath+"_"+color+".png");
}else{
targetFile = new File(targetPath);
if(!targetFile.exists()){
File dir = new File(targetFile.getParent());
if(!dir.exists()) dir.mkdirs();
}
}
ImageIO.write(bufferedImage, "png", targetFile);
} catch (Exception e) {
e.printStackTrace();
}
return byteArrayOutputStream.toByteArray();
}
/**
* 检查颜色是否为 白色 或者 黑色阈值范围
* @param rgb 颜色值
* @param color 0:白色 1:黑色
* @return 检查结果
*/
private static boolean checkColor(int rgb,int offset,int color){
int R = (rgb & 0xff0000) >> 16;
int G = (rgb & 0xff00) >> 8;
int B = (rgb & 0xff);
if(color == 0){
return ((255 - R) <= offset) && ((255 - G) <= offset) && ((255 - B) <= offset);
}else{
return ((R <= offset) && (G <= offset) && (B <= offset));
}
}
public static void main(String[] args) throws IOException {
Image src = ImageIO.read(new File("C://Users//Mr.Ma//Desktop//20190616132703.png"));
BufferedImage bufferedImage = Rotate(src,90);
ImageIO.write(bufferedImage, "png", new File("C://Users//Mr.Ma//Desktop//3.png"));
//去除影像黑边
transferAlpha("C://Users//Mr.Ma//Desktop//3.png","C://Users//Mr.Ma//Desktop//3.png",1);
System.out.printf("生成完毕");
}
}