简体   繁体   中英

Android: How to change the orientation of an image to set it in imageview?

I am setting an image to ImageView, however the image is not picked from the device storage, the image is sent by the server, I want to know about the orientation of the image before setting it to the imageview. So, is it possible to check the orientation the jpg image coming from the server using its url and ExifInterface and then change its orientation as per required to set to the imageview?

No determining orientation is not possible. You will have to check the width and height and align the images accordingly to your ImageView.

Maybe this could help you

void readAndDisplayMetadata( String fileName ) {
    try {

        File file = new File( fileName );
        ImageInputStream iis = ImageIO.createImageInputStream(file);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);

        if (readers.hasNext()) {

            // pick the first available ImageReader
            ImageReader reader = readers.next();

            // attach source to the reader
            reader.setInput(iis, true);

            // read metadata of first image
            IIOMetadata metadata = reader.getImageMetadata(0);

            String[] names = metadata.getMetadataFormatNames();
            int length = names.length;
            for (int i = 0; i < length; i++) {
                System.out.println( "Format name: " + names[ i ] );
                displayMetadata(metadata.getAsTree(names[i]));
            }
        }
    }
    catch (Exception e) {

        e.printStackTrace();
    }
}

http://johnbokma.com/java/obtaining-image-metadata.html

Note: If you are sure that image is portrait if and only if your images height is higher than its width, you could measure them to find its orientation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM