0

I'm using ImageCapture api to capture a screenshoot from a camera of a user device.

The problem is that photos taken on android get rotated incorrectly. They are rotated incorrectly both in the photo preview, before the image is uploaded, and after it is uploaded.

My plan was to read the exif data from a blob that I get from image capture, find orientation in exif and use javascript to rotate the image correctly, but there is no usable exif data because it isnt jpg.

My code:

function takePhoto(img) { 
imageCapture.takePhoto()
.then(blob => {
  // EXIF.getData(blob, function() {
  //      myData = this;
  //      console.log( myData );
  //   });
    let url = window.URL.createObjectURL(blob);
    img.src = url;
    console.log( blob );
    // stop the camera and hide canavas
    stream.getVideoTracks()[0].stop();
    $("#webcam").hide();
    image = blob;
    $("#photo-info").slideDown('slow');
})
.catch(function (error) {
    console.log(error);
});
}; 

takePhoto(document.querySelector('#camera-feed'));

Is there a way to detect if a image is rotated incorrectly when using ImageCapture?

failedCoder
  • 1,346
  • 1
  • 14
  • 38

2 Answers2

0

It depends on the device you are using, you should deal with the picture after taken in two steps:

  1. Validate if the photo is rotated.
  2. If necesary rotate the photo.

To validate if the photo is rotated you can use ExifInterface and to rotate you can use a matrix to invert it playing with the bits

You can find more related information and code example at this question

0

I could not get this working, along with some other problems, so I decided to ditch ImageCapture all together and go with this:

<input type="file" accept="image/*" capture>

This opened the device camera as soon as user clicked on the input button, which is exacly what I needed, but since the taken photo came with exif on android I could correctly rotate it.

For the rotation itself, I used this library

failedCoder
  • 1,346
  • 1
  • 14
  • 38