Java画图时产生图片翻转

本文介绍了一种解决从iOS设备获取的照片在Java程序中显示时出现翻转问题的方法。通过使用thumbnailator和metadata-extractor库读取图片元数据并根据图片方向进行相应的角度调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java画图时,若使用苹果类型的手机拍摄的照片,画图产生翻转解决如下:

// https://mvnrepository.com/artifact/net.coobird/thumbnailator

compile group: 'net.coobird', name: 'thumbnailator', version: '0.4.8'
// https://mvnrepository.com/artifact/com.drewnoakes/metadata-extractor

compile group: 'com.drewnoakes', name: 'metadata-extractor', version: '2.11.0'

 

 

private InputStream getImage(String imagePath) throws Exception {
    URL url = new URL(imagePath);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();// 打开URL

    return urlConnection.getInputStream();
}

//针对图片信息翻转
private int getImageAngle(InputStream inputStream) throws Exception {
    Metadata metadata = JpegMetadataReader.readMetadata(inputStream);
    Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    int angel = 0;
    if (directory != null){
        if(directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)){
            int orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
            // 原图片的方向信息
            if(6 == orientation ){
                //6旋转90
                angel = 90;
            }else if( 3 == orientation){
                //3旋转180
                angel = 180;
            }else if( 8 == orientation){
                //8旋转90
                angel = 270;
            }
        }
    }
    return angel;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值