-2

Helo community,

I've got a problem. Please help me.

I chosse a picture from intent like this:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            // We need to recyle unused bitmaps
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = null;
            try {
                // Von Gallerie


                System.out.println("Test A1");

                Bitmap bitmap = null;
                    stream = getContentResolver().openInputStream(
                        data.getData());
                    bitmap = BitmapFactory.decodeStream(stream);
                stream.close();

                this.UPLOAD_URL = Config.webSiteUrl + "?action=uploadFile&username=" + this.username + "&password=" + this.password + "&baustelleid=" + Fotos.this.baustelleid;

               // System.out.println("xyy: " + this.UPLOAD_URL);

                bitmap = scaleDown(bitmap, Config.maxImageUploadSize, true);


                if(bitmap != null) {
                    uploadImage(bitmap, this.UPLOAD_URL);
                }
            } catch (IOException e1) {
              //  System.out.println("Fehler 2");
            }
        } catch (Exception e) {
         //   System.out.println("Fehler 1");
        }
}

My problem is, that some JPEG images have got EXIF-Headers, which contain a rotation.

When i display my image, the problem is, that it is turned 180 degrees around.

How can i rotate the bitmap 180 degrees, so it is displayed the right way?

(Sorry for bad english :-) )

Edit:

The EXIF-code of the jpeg file, which is wrong around, is:

20180712_101743.jpg: JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=12, height=3096, manufacturer=samsung, model=SM-A310F, orientation=lower-right, xresolution=210, yresolution=218, resolutionunit=2, software=A310FXXU3CQL2, datetime=2018:07:12 10:17:43, width=4128], baseline, precision 8, 4128x3096, frames 3

Other pictures, which are the right way:

20180712_171712.jpg: JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=12, datetime=2018:07:12 17:17:13, model=SM-A310F, resolutionunit=2, height=0, yresolution=187, orientation=[0], software=A310FXXU3CQL2, xresolution=209, manufacturer=samsung, width=0], baseline, precision 8, 4096x2606, frames 3

ubik
  • 37
  • 4

2 Answers2

1

Once you have the bitmap, you can also create a rotated copy of it like this

bitmap = scaleDown(bitmap, Config.maxImageUploadSize, true);

Matrix mtx = new Matrix();
mtx.postRotate(180f);

Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);
Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • Helo, but that's not my goal to rotate **all** pictures. My problem is, that some images from gallery are uploaded in the right rotation, others not. I guess that there is an EXIF information which tells me, when to rotate. – ubik Jul 21 '18 at 11:08
  • If you want to read the orientation from EXIF data, see this answer (https://stackoverflow.com/a/11081918/9473786). Then you can use that to figure out how much to rotate the image. – Tyler V Jul 21 '18 at 12:58
  • Okay, I tried rotating by reading EXIF data. Still it doesn't work. My problem is, that in the gallery the rotation is in the right way. Now, when I upload the picture into my app, it is 180 degrees around. I do the upload with volley, so I encode it into base64 and read it with PHP from $_POST[] and I decode it with PHP. This is the pic: https://62.113.211.136/nextcloud/index.php/s/Nq4ZnCWB7oHDCc8 . What possible reason could it be? – ubik Jul 22 '18 at 11:35
0

After you detect that the image needs or not rotation, store this state in a boolean variable needsRotation and rotate or not the ImageView where you show it:

    ExifInterface ex = new ExifInterface(uri.getPath());
    int rotation = ex.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    Boolean needsRotation = (rotation != ExifInterface.ORIENTATION_NORMAL));
    if (needsRotation) {
        imageView.setRotation(180f);
    } else {
        imageView.setRotation(0f);
    }
  • Then I would have the problem, that images, which doesnt need to be rotated, are also rotated, too. – ubik Jul 22 '18 at 11:36
  • Can you detect if the image needs rotation? –  Jul 22 '18 at 11:37
  • Helo, I read the EXIF information of this picture. It is stored in little-endian format. The other pictures are stored in big endian format. How can I detect, if the jpeg is big endian or little endian? I think, that's the reason – ubik Jul 22 '18 at 11:59
  • You can read this [link](https://stackoverflow.com/questions/7286714/android-get-orientation-of-a-camera-bitmap-and-rotate-back-90-degrees/11081918#11081918) –  Jul 22 '18 at 12:04
  • And what is the constant for reading EXIF big/little endian format? – ubik Jul 22 '18 at 12:08
  • Which constant? –  Jul 22 '18 at 12:09
  • I need a constant variable for the ExifInterface to get information if the jpeg file is encoded in little endian or big endian format. (It's not the orientation) – ubik Jul 22 '18 at 12:11
  • mTak: Yes I tried this. But there is no effect. It always goes to the else-branch. Look at my post at the top. I added the EXIF-information of the file, which doesn't work and a file which does work. – ubik Jul 22 '18 at 13:10
  • Are you sure that the rotation needed is 180 because it could be 90 or 270 in which case the code would lead to the else part –  Jul 22 '18 at 13:12
  • Yes, I'm sure. Maybe it's not the rotation, but the little endian and big endian format? Look at the exif information at my first post at the top. (with little endian the words are stored wrong around) – ubik Jul 22 '18 at 13:14
  • How did you get the EXIF-code of the images as you wrote them in your question? –  Jul 22 '18 at 13:19
  • This was with a linux command. For example 'file 20180712_101743.jpg'. It display all information about a file. – ubik Jul 22 '18 at 13:27
  • As you see there in big-endian info orientation=0 is present, but in little-endian there is no mention about this, so I can't see how you can get this info –  Jul 22 '18 at 13:33
  • When I upload the file, i use a base64-encoding like this: String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); So my suggestion is, that a little-endian encoded jpeg image is encoded the wrong way round. Do you know what little-endian and big-endian means? Look at https://en.wikipedia.org/wiki/Endianness . Big Endian stores the bit-words in the way they come in. Little-endian stores the bit-words the other way round. So my suggestion why the picture is rotated 180 degrees is, that it uses little_endian. So I just need to know, if the jpeg file is litte- or bigendian – ubik Jul 22 '18 at 15:26
  • I tried, but it still goes to the second branch (else-branch) – ubik Jul 23 '18 at 07:50
  • I edited my answer. Can you please try the above for several of your pics? If it always goes to the else statement then this means that you can't get the orientation. –  Jul 23 '18 at 08:48
  • Yes, this did it! Thank you very much! :-) – ubik Jul 23 '18 at 11:12
  • I'm glad, be well –  Jul 23 '18 at 11:13