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