Java reads pictures and EXIF information

Source: Internet
Author: User

Background needs to read the client upload pictures, record the image length and width, the client can later according to the length of the record, display pictures.

Normal picture, reading is very simple, the code is as follows:

BufferedImage originalimg =int originalwidth = Originalimg.getwidth ();  // the width of the uploaded image int originalheight = Originalimg.getheight ();  // upload pictures of high

However, there is a situation where the client takes photographs that are likely to be flipped in the direction, wide and high, but higher than wide. Whether it is the code above or directly in Windows to view the picture, but also get a large and high information.

With the above directly read and high code, get 4128x2322, this value does not consider the picture flipping direction, so is wrong. The correct approach is to read the EXIF information of the picture, depending on its flip angle, to determine the width and height.

Metadata Metadata =jpegmetadatareader.readmetadata (file);D irectory Directory= Metadata.getdirectory (exifdirectory.class);if(Directory.containstag (exifdirectory.tag_orientation)) {//The EXIF information is saved in the direction, the information is copied to the thumbnail image    intOrientation = Directory.getint (exifdirectory.tag_orientation);//Orientation information for the original picture   if(6 = = Orientation | | 8 = = orientation) {//Flip the 90° and 270°, the length-width swap       intOriginalWidth = Originalimg.getheight ();//the width of the uploaded image      intOriginalHeight = Originalimg.getwidth ();//upload pictures of high}}

Java reads pictures and EXIF information

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.