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;
}