Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - How to take a picture in webcam with EXIF data?

I am currently using webcam (not native camera) on a web page to take a photo on users' mobile phone. Like this:

var video: HTMLVideoElement;
...
var context = canvas.getContext('2d');
context.drawImage(video, 0, 0, width, height);
var jpegData = canvas.toDataURL('image/jpeg', compression);

In such a way, I can now successfully generate a JPEG image data from web camera, and display it on the web page.

However, I found that the EXIF data is missing. according to this:

Canvas.drawImage() will ignore all EXIF metadata in images, including the Orientation. This behavior is especially troublesome on iOS devices. You should detect the Orientation yourself and use rotate() to make it right.

I would love the JPEG image contain the EXIF GPS data. Is there a simple way to include camera EXIF data during the process?

Thanks!

like image 791
RainCast Avatar asked Oct 17 '25 04:10

RainCast


2 Answers

Tested on Pixel 3 - it works. Please note - sometimes it does not work with some desktop web-cameras. you will need exif-js to get the EXIF object from example.

  const stream = await navigator.mediaDevices.getUserMedia({ video : true });
  const track = stream.getVideoTracks()[0];
  let imageCapture = new ImageCapture(track);
  imageCapture.takePhoto().then((blob) => {
      const newFile = new File([blob], "MyJPEG.jpg", { type: "image/jpeg" });
      EXIF.getData(newFile, function () {
          const make = EXIF.getAllTags(newFile);
          console.log("All data", make);
     });
  });

Please check the image with EXIF data

like image 161
jsDev Avatar answered Oct 19 '25 17:10

jsDev


unfortunately there's no way to extract exif from canvas.

Although, if you have access to jpeg, you can extract exif from that. For that I'd recommend exifr instead of widely popular exif-js because exif-js has been unmaintained for two years and still has breaking bugs in it (n is undefined).

With exifr you can either parse everything

exifr.parse('./myimage.jpg').then(output => {
  console.log('Camera:', output.Make, output.Model))
})

or just a few tags

let output = await exifr.parse(file, ['ISO', 'Orientation', 'LensModel'])
like image 25
Mike Kovařík Avatar answered Oct 19 '25 17:10

Mike Kovařík



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!