Joko Joko
Joko Joko

Reputation: 121

React-Native-Camera get height and width from uri

I have an app, which takes a photo and add it to the pdf file. Problem is resizing. I can resize it with pixels, but that not keep the original ratio. I need original height and width for calculating the right size

Calculate: Divide height by width, example 1200/1600 = 0,75. Then we can resize pd image height 100px and width is 100 / 0,75.

Question is: How can I get the size of image (data.uri)?

My code:

takePicture = async() => {
    
    if (this.camera) {
        const options = { quality: 0.5, base64: true , fixOrientation: true}; // Option for wuality etc
        const data = await this.camera.takePictureAsync(options); // Take the image
        console.log(data.uri); // Prints image (data) address to console log
        back(data.uri) // Go back to ReportFault page
    }
  };
    let imgX = 297;
    let imgY = 618;

page.drawImage(arr[i].path.substring(7),'jpg',{
     x: imgX, // Position in pdf A4
     y: imgY, // Position in pdf A4
     width: 200,
     height: 150,
                })

Upvotes: 1

Views: 1956

Answers (1)

Mehran Khan
Mehran Khan

Reputation: 3636

you can use react native Image getSize method

try this

import { Image } from 'react-native';

.....

Image.getSize(uri, (width, height) => {
  console.log(`The image dimensions are ${width}x${height}`);
}, (error) => {
  console.error(`Couldn't get the image size: ${error.message}`);
});

Upvotes: 3

Related Questions