React-Native-Camera 从 uri 获取高度和宽度

React-Native-Camera get height and width from uri

我有一个应用程序,可以拍照并将其添加到 pdf 文件中。问题是调整大小。我可以用像素调整它的大小,但不能保持原来的比例。我需要原始高度和宽度来计算正确的尺寸

计算:高度除以宽度,例如 1200/1600 = 0,75。然后我们可以调整pd图像的高度为100px,宽度为100 / 0,75。

问题是:如何获取图像的大小(data.uri)

我的代码:

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,
                })

你可以使用 react native Image getSize 方法

试试这个

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}`);
});