Hello Guys,
Here I will suggest you the best coding standard to develop effective and fast application in java technology. So, now I am writing code for changing image orientation while uploading image from mobile.
For this follow below steps -
step 1.
Import below .jar file.
imgscalr-lib-4.2.jar
metadata-extractor-2.6.2.jar
step 2.
InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream(parameterName));
BufferedImage sourceImage = ImageIO.read(in);
//Invoke getEXIFOrientation method
Rotation rotate=getEXIFOrientation(IOUtils.toByteArray(uploadRequest.getFileAsStream(parameterName)));
if(ratate != null){
//Rotate to image
sourceImage = Scalr.rotate(sourceImage,rotate , Scalr.OP_ANTIALIAS);
}
//Note : "sourceImage" to change according requirement
private static Rotation getEXIFOrientation(byte[] bytes) {
int orientation = 1; // default = unknown
ByteArrayInputStream bis = null;
try {
bis = new ByteArrayInputStream(bytes);
Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(bis), false);
ExifIFD0Directory exifDir = metadata.getDirectory(ExifIFD0Directory.class);
if (exifDir != null) {
orientation = exifDir.getInt(ExifIFD0Directory.TAG_ORIENTATION); // 274 is the EXIF orientation standard code
}
} catch (Exception e) {
System.out.println("Couldn't extract EXIF orientation from image");
} finally {
if (bis != null) try {
bis.close();
} catch (IOException e) {
// nothing
}
}
switch (orientation) {
case 1: // [Exif IFD0] Orientation - Top, left side (Horizontal / normal)
return null;
case 6: // [Exif IFD0] Orientation - Right side, top (Rotate 90 CW)
return Rotation.CW_90;
case 3: // [Exif IFD0] Orientation - Bottom, right side (Rotate 180)
return Rotation.CW_180;
case 8: // [Exif IFD0] Orientation - Left side, bottom (Rotate 270 CW)
return Rotation.CW_270;
}
return null;
}
Thank you
0 Comment(s)