原创

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

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

/**
 * @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度的问题。

正文到此结束
本文目录