前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA后台处理解决苹果手机IOS上传图片旋转90度问题

JAVA后台处理解决苹果手机IOS上传图片旋转90度问题

作者头像
一诺千金
发布2020-04-30 00:52:09
1.5K0
发布2020-04-30 00:52:09
举报
文章被收录于专栏:郭家一诺千金郭家一诺千金

JAVA后台处理解决苹果手机IOS上传图片旋转90度问题 在做项目的时候遇到问题,通过苹果手机iphone(IOS)上传图片到服务器,后端得到的图片不是正常的。于是找到一个工具类,依赖我的业务进行简单的处理,记录下来,在此与大家共享,以便需要用时可以直接拿来用,上干货。

代码语言:javascript
复制
/**
 * @Author: guo
 * @Description: Java处理ios图片旋转的问题
 * @Date: 2019/5/6 11:56
 * @Version: 1.0
 */
public class RotateUtil {
    public static File getPicture(File file){
    try {
        BufferedImage src = getPicture(file);
        BufferedImage bi = null;
        if(src != null){
            int angel = getRotateAngle(file);//得到图片旋转角度
            if(angel == 0){
                bi = file;//图片正常直接返回
            }else{
                int srcWidth = src.getWidth(null);
                int srcHeight = src.getHeight(null);
                Rectangle rectangle = getRotatedSize(new Rectangle(new Dimension(srcWidth, srcHeight)), angel);
                bi = new BufferedImage(rectangle.width, rectangle.height,BufferedImage.TYPE_INT_RGB);
                Graphics2D g2 = bi.createGraphics();
                g2.translate((rectangle.width - srcWidth) / 2,
                        (rectangle.height - srcHeight) / 2);
                g2.rotate(Math.toRadians(angel), srcWidth / 2, srcHeight / 2);
                g2.drawImage(src, null, null);
            }
            int index = file.getName().lastIndexOf(".");
            String formate = file.getName().substring(index+1);
            ImageIO.write(bi, formate, file);
            return file;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}


    /**
     * 读取图片文件
     */
    public static BufferedImage getPicture(File file) {
        BufferedImage bi = null;
        try{
            if(!file.exists()){
                return null;
            }
            bi = ImageIO.read(file);
        } catch (Exception e){
            e.printStackTrace();
        }
        return bi;
    }


    /**
     * 计算图片翻转到正常显示需旋转角度
     */
    public static int getRotateAngle(File file){
        int angel = 0;
        Metadata metadata=null;
        try{
            metadata = ImageMetadataReader.readMetadata(file);
            int orientation = 0;
            Iterable<Directory> iterable = metadata.getDirectories();

            for (Iterator<Directory> iter = iterable.iterator(); iter.hasNext(); ) {
                Directory dr = iter.next();
                if (dr.getString(ExifIFD0Directory.TAG_ORIENTATION) != null) {
                    orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION);
                }
                /*Collection<Tag> tags = dr.getTags();
                for (Tag tag : tags) {
               System.out.println(tag.getTagName() + ": " + tag.getDescription());
            }*/
            }
            if (orientation == 0 || orientation == 1) {
                angel = 360;
            } else if (orientation == 3) {
                angel = 180;
            } else if (orientation == 6) {
                angel = 90;
            } else if (orientation == 8) {
                angel = 270;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return angel;
    }


    /**
     * 计算旋转参数
     */
    public static Rectangle getRotatedSize(Rectangle src,int angel){
        // if angel is greater than 90 degree,we need to do some conversion.
        if(angel > 90){
            if(angel / 9%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 angelAlpha = (Math.PI - Math.toRadians(angel)) / 2;
        double angelDaltaWidth = Math.atan((double) src.height / src.width);
        double angeDaltaHeight = Math.atan((double) src.width / src.height);

        int lenDaltaWidth = (int) (len * Math.cos(Math.PI - angelAlpha
                - angelDaltaWidth));
        int lenDaltaHeight = (int) (len * Math.cos(Math.PI - angelAlpha
                - angeDaltaHeight));
        int desWidth = src.width + lenDaltaWidth * 2;
        int desHeight = src.height + lenDaltaHeight * 2;
        return new Rectangle(new Dimension(desWidth, desHeight));
    }
}

至此,JAVA WEB后台就处理解决了苹果手机IOS上传图片旋转90度的问题。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年12月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档