Ashampoo / kim

Image metadata manipulation library for Kotlin Multiplatform
https://ashampoo.github.io/kim/
Apache License 2.0
155 stars 6 forks source link

Android phones do not give TiffTag for rotation #99

Open JakkeJ opened 2 months ago

JakkeJ commented 2 months ago

I have rotated pictures in regular Compose/Kotlin before, and gotten the Exif orientation value out, but there is nothing here, so this is pretty useless for rotating pictures the right way up. This happens on using webcamera on emulator as well as a real Sony Xperia 1V device. On iPad I get orientation 1 out, so that works, but I am depending on getting it out on all devices...

Any tips are welcome.

JakkeJ commented 2 months ago

2024-05-12 21:33:05.605 10872-10872 rotateImage com.sar.app.android D Orientation: null

from

fun rotateImage(mapViewModel: MapViewModel) {
    val inputFile: ByteArray = mapViewModel.imageByteArray ?: return
    val metadata = Kim.readMetadata(inputFile)
    val orientation = metadata?.findShortValue(TiffTag.TIFF_TAG_ORIENTATION)
    val test = metadata?.findDoubleValue(ExifTag.)
    val log = logging("rotateImage")
    log.d { "Orientation: $orientation" }
    val newBytes = Kim.update(
        bytes = inputFile,
        update = MetadataUpdate.Orientation(TiffOrientation.MIRROR_HORIZONTAL_AND_ROTATE_RIGHT)
    )
    mapViewModel.imageBitmap = toImageBitmap(newBytes)
}
StefanOltmann commented 2 months ago

Hi,

thanks for reaching out.

So you have different results for the same file on different platforms? As most of the code is shared code this shouldn’t happen.

Can you share a sample file for further investigation?

I like to inspect it using https://stefan-oltmann.de/exif-viewer which has a useful HEX view and is also powered by this library.

Kind regards,

Stefan

JakkeJ commented 2 months ago

Hi Stefan! No, it is when I take a picture with the phones/tablets. I used Exif data to get rotation in Compose a year ago, and that worked fine, we could rotate the image so it always was the right way up. This time, we are using Compose Multiplatform, so Android Graphics do not work, and ExifInterface also gives me orientation 0 on the android side of things when I made an expect/actual function with Kim on iOS and ExifInterface on Android.

In other words, I am confused, as I did get the orientation out from my phone last year, I wonder if something is lost in translation from jpg to inputstream, or if there is something else going on that I don't quite understand.

StefanOltmann commented 2 months ago

Yes, it’s possible that grabbing a picture from camera input just gives you JPEG bytes without any metadata. I believe it is the camera app itself that adds the EXIf to the file. So you would need to detect the phone rotation when grabbing the image bytes and write that value as the orientation tag.

You can check this if you save the bytes you got from taking the picture to a file and get that off your phone using adb pull. Inspect those bytes using https://stefan-oltmann.de/exif-viewer and/or ExifTool by Phil Harvey.

If metadata is present and Kim can’t read it this is a bug I need to fix. If it’s not there (what I think is highly likely) the output you got is correct.

As you say you got the orientation out last year, do you mean a older version of this library works and something broke in the current version?

StefanOltmann commented 1 month ago

@JakkeJ Did you figure out the source of the problem?

JakkeJ commented 1 month ago

@JakkeJ Did you figure out the source of the problem?

I actually used some old code from an old mobile programming course project to load the pictures in, either newly taken or from gallery, and when I did that I got the exif data no problem. Since it worked fine on iOS pictures that sorted me out.

StefanOltmann commented 1 month ago

@JakkeJ You said also ExifInterface did not work for you.

Can you share the code that works for you and gets the orientation right? I still don’t understand the source of the problem and what I can do here.

JakkeJ commented 1 month ago

@JakkeJ You said also ExifInterface did not work for you.

Can you share the code that works for you and gets the orientation right? I still don’t understand the source of the problem and what I can do here.

Sorry, I'm in the middle of moving right now so I don't have access to my computers for a while. So I'll try to explain:

Not entirely sure it was on your end tbf, the way the code to take pictures I used before, created an empty png file I believe, and then added the data to it, most likely this didn't include the exif data which was never added, seemingly. My old code from last year just did this process differently, and exif data was included. As said earlier it was only on Android it happened, and I followed some guide, probably from Medium, on how to take pictures in Kotlin Multiplatform.