IOS设备上传图片,使用ImageIO.write 图片翻转纠正(JAVA)

  在使用ios设备时,上传图片,发现图片旋转了90度。
 
正确:
 
错误:

1. 引入pom

pom.xml中引入需要的依赖内容:

 

        <dependency>
            <groupId>com.drewnoakes</groupId>
            <artifactId>metadata-extractor</artifactId>
            <version>2.7.0</version>
        </dependency>
 

2. 计算图片翻转到正常显示需旋转的角度数

 1   /**
 2      * 计算图片翻转到正常显示需旋转角度
 3      */
 4     public static int getRotateAngle(File file) {
 5 
 6         int angel = 0;
 7         Metadata metadata = null;
 8         try {
 9             metadata = ImageMetadataReader.readMetadata(file);
10             int orientation = 0;
11             Iterable<Directory> iterable = metadata.getDirectories();
12 
13             for (Iterator<Directory> iter = iterable.iterator(); iter.hasNext(); ) {
14                 Directory dr = iter.next();
15                 if (dr.getString(ExifIFD0Directory.TAG_ORIENTATION) != null) {
16                     orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION);
17                 }
18                 Collection<Tag> tags = dr.getTags();
19                 for (Tag tag : tags) {
20                     System.out.println(tag.getTagName() + ": " + tag.getDescription());
21                 }
22             }
23             System.out.println("orientation::::" + orientation);
24             if (orientation == 0 || orientation == 1) {
25                 angel = 360;
26             } else if (orientation == 3) {
27                 angel = 180;
28             } else if (orientation == 6) {
29                 angel = 90;
30             } else if (orientation == 8) {
31                 angel = 270;
32             }
33         } catch (Exception e) {
34             e.printStackTrace();
35         }
36         return angel;
37     }

 


3. 旋转图片-指定度数

 

 1 /**
 2      * 旋转图片
 3      */
 4     public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) {
 5         if (bufferedImage == null) {
 6             return null;
 7         }
 8         if (angel < 0) {
 9             // 将负数角度,纠正为正数角度
10             angel = angel + 360;
11         }
12         int imageWidth = bufferedImage.getWidth(null);
13         int imageHeight = bufferedImage.getHeight(null);
14         // 计算重新绘制图片的尺寸
15         Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);
16         // 获取原始图片的透明度
17         int type = bufferedImage.getColorModel().getTransparency();
18         BufferedImage newImage = null;
19         newImage = new BufferedImage(rectangle.width, rectangle.height, type);
20         Graphics2D graphics = newImage.createGraphics();
21         // 平移位置
22         graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
23         // 旋转角度
24         graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);
25         // 绘图
26         graphics.drawImage(bufferedImage, null, null);
27         return newImage;
28     }

 


 

将图片保存到本地:

private InputStream savePic(InputStream inputStream, String path) {

        OutputStream os = null;
        try {
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            os = new FileOutputStream(path);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            InputStream input = new FileInputStream(path);
            return input;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

 

 

/**
     * 计算旋转后的尺寸
     * 
     * @param src
     * @param angel
     * @return
     */
    private static Rectangle calculatorRotatedSize(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));
    }

 

 注:需将图片正常保存到本地,然后使用 getRotateAngle 方法获取需要旋转到角度

 

//先把图片存到本地
InputStream inputStream = savePic(file.getInputStream(),"/test/a.jpg");
//获取本地图片需要旋转到度数
int rotateAngle = getRotateAngle(new file("/test/a.jpg"));
//读取BuffereadImage. BufferedImage bi
= ImageIO.read(inputStream); //如果需要旋转 if (rotateAngle != 0) {
  //旋转 bi
= rotateImage(bi, rotateAngle); }
//最后使用
ImageIO.write(bi, "jpg", file);
 

 

posted @ 2020-04-06 09:06  Timesyys  阅读(1747)  评论(3编辑  收藏  举报