Java solves the problem of uploading photos to the server in mobile devices such as phones

Source: Internet
Author: User

If you upload a photo from a mobile device such as a phone to a server and then display an uploaded image, there may be an incorrect orientation, because the photo contains orientation information that tells you where the photo is, but if we don't handle the direction, the display will be incorrect. If you read the width and height of the uploaded photos in the normal way:

BufferedImage image = Imageio.read (new File (FullPath));
int width = image.getwidth (); The width of the picture
int height = height.getheight (); Picture of the High

This obtains the width and height, may not be correct, because does not consider the picture direction information, simply said, I found that if is the handset, will the handset rotates 90 degrees clockwise to become the horizontal screen, at this time the photograph, shines the picture, uploads the server to be normal, namely, at this time the picture direction information is the standard direction, If you are using a normal vertical screen, you need to rotate the photo 90 degrees to the same direction as when you take the photo, the same way, there are two directions you need to rotate the photos 180 degrees and 270 degrees to display correctly.

The direction of the picture information in the image file EXIF information, so long as the image file to read EXIF information, you can know the angle of rotation, then after the upload is completed, the picture is rotated again by the angle to write to the server, not normal, I solve the problem of the idea is this.

Then, to obtain the EXIF information of the picture, the need to use Mediautil-1.0.jar, Metadata-extractor-2.3.1.jar, has been uploaded to the attachment. Get the angle that you want to rotate, code:

/**
* Get the picture correctly display the angle that needs to be rotated (clockwise)
* @return
*/
public static int Getrotateangleforphoto (String filePath) {

File File = new file (FilePath);

int angle = 0;

Metadata Metadata;
try {
metadata = jpegmetadatareader.readmetadata (file);
Directory directory = metadata.getdirectory (Exifdirectory.class);
if (Directory.containstag (exifdirectory.tag_orientation)) {

Direction of EXIF Information
int orientation = Directory.getint (exifdirectory.tag_orientation);

Orientation information for the original picture
if (6 = = orientation) {
6 Rotation 90
angle = 90;
}else if (3 = = orientation) {
3 Rotation 180
angle = 180;
}else if (8 = = orientation) {
8 Rotation 90
angle = 270;
}
}
} catch (Jpegprocessingexception e) {
E.printstacktrace ();
} catch (Metadataexception e) {
E.printstacktrace ();
}

return angle;
}

After getting to the angle that needs to be rotated, after uploading, add a step to rotate the operation, regenerate the picture again:

/**
* Rotate Mobile Photos
* @return
*/
public static string Rotatephonephoto (string fullPath, int angel) {

BufferedImage src;
try {
src = imageio.read (new File (FullPath));

int src_width = Src.getwidth (null);
int src_height = Src.getheight (null);

Rectangle rect_des = calcrotatedsize (new Rectangle (New Dimension (Src_width, src_height)), Angel);

BufferedImage res = new BufferedImage (rect_des.width, RECT_DES.HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);
graphics2d g2 = Res.creategraphics ();

G2.translate ((rect_des.width-src_width)/2,
(rect_des.height-src_height)/2);
G2.rotate (Math.toradians (Angel), SRC_WIDTH/2, SRC_HEIGHT/2);

G2.drawimage (SRC, null, NULL);

Imageio.write (res, "JPG", new File (FullPath));

} catch (IOException e) {

E.printstacktrace ();
}

return fullPath;

}


In this way, the mobile phone photo upload direction is normal.

This article is from the "Thatway" blog, make sure to keep this source http://thatway.blog.51cto.com/4815281/1627283

Java solves the problem of uploading photos to the server in mobile devices such as phones

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.