for (int j1 = bufferedImage.getMinY(); j1bufferedImage
.getHeight(); j1++) {
for (int j2 = bufferedImage.getMinX(); j2bufferedImage
.getWidth(); j2++) {
int rgb = bufferedImage.getRGB(j2, j1);
int R = (rgb0xff0000)16;
int G = (rgb0xff00)8;
int B = (rgb0xff);
if (((255 - R)range)((255 - G)range)((255 - B)range)) { //去除白色背景;
rgb = ((alpha + 1)24) | (rgb0x00ffffff);
} else {
minX = minX = j2 ? minX : j2;
minY = minY = j1 ? minY : j1;
maxX = maxX = j2 ? maxX : j2;
maxY = maxY = j1 ? maxY : j1;
}
bufferedImage.setRGB(j2, j1, rgb);
}
}
int width = maxX - minX;
int height = maxY - minY;
BufferedImage sub = bufferedImage.getSubimage(minX, minY, width, height);
int degree = getDegree(in);
sub = rotateImage(sub,degree);
ImageIO.write(sub, "png", byteArrayOutputStream);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return byteArrayOutputStream.toByteArray();
}
/**
* 图片旋转
* @param bufferedimage bufferedimage
* @param degree 旋转的角度
* @return BufferedImage
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
w, h)), degree);
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(rect_des.width, rect_des.height, type))
【java中抠图代码 java绘制图片代码】.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.translate((rect_des.width - w) / 2,
(rect_des.height - h) / 2);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 计算旋转后图像的大小
* @param srcRectangle
* @param degree 旋转的角度
* @return Rectangle
*/
public static Rectangle CalcRotatedSize(Rectangle src, int degree) {
if (degree = 90) {
if(degree / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
degree = degree % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(degree) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(degree)) / 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));
}
/**
* byte[] ------BufferedImage
*
* @param byteImage byteImage
* @return return
* @throws IOException IOException
*/
public static BufferedImage ByteToBufferedImage(byte[] byteImage) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(byteImage);
return ImageIO.read(in);
}
/**
* 获取照片信息的旋转角度
* @param inputStream 照片的路径
* @return 角度
*/
public static int getDegree(InputStream inputStream) {
try {
Metadata metadata = https://www.04ip.com/post/ImageMetadataReader.readMetadata(new BufferedInputStream(inputStream),true);
推荐阅读
- 成都正规网站搭建,成都网站建设公司哪家便宜
- qq电脑传的文件ios哪里,电脑传的文件ios哪里打开
- 鸿蒙系统蓝牙耳机音质,鸿蒙系统蓝牙耳机音量小
- linux命令日志,linux系统日志设置
- php中的数据结构 php数组的数据结构
- 什么叫cpu温度,cpu温度过高是什么原因
- chatgpt还要学英语吗,cgtn学英语可以吗
- 中班平衡木体育游戏反思,中班平衡木游戏目标
- term函数python tempstr函数python