2

我不知道如何使用react-native-camera捕获方法获取捕获的图像的元数据。

我假设它是这样的:

this.camera.capture({metadata: true})
  .then((data) => console.log(data))
  .catch(error => alert("error: " + error));

但它只返回路径,所以:

this.camera.capture({metadata: 'location'})
  .then((data) => console.log(data))
  .catch(error => alert("error: " + error));

我不清楚文档元数据的使用情况。传递元数据参数的正确方法是什么?有没有人让这个工作?

4

1 回答 1

2

这可能是在黑暗中拍摄,但如果您阅读 README.md https://github.com/lwansbrough/react-native-camera的第一段,它会说:

这些文档适用于进行中的 v1 版本。如果你想使用最新最好的并且可以处理严重的不稳定性,你可以使用 npm install --save lwansbrough/react-native-camera 安装。

该库的 NPM 版本为 0.3.8:https ://www.npmjs.com/package/react-native-camera 。

编辑

所以我去在 0.3.8 安装库并node_modules查看 Java 代码(假设你在 Android 手机上测试它)并搜索本机capture函数,实际上没有任何东西可以处理该metadata选项,这很可悲. 尝试搜索metadata,您将找不到任何东西:

@ReactMethod
public void capture(final ReadableMap options, final Promise promise) {
    int orientation = options.hasKey("orientation") ? options.getInt("orientation") : RCTCamera.getInstance().getOrientation();
    if (orientation == RCT_CAMERA_ORIENTATION_AUTO) {
        _sensorOrientationChecker.onResume();
        _sensorOrientationChecker.registerOrientationListener(new RCTSensorOrientationListener() {
            @Override
            public void orientationEvent() {
                int deviceOrientation = _sensorOrientationChecker.getOrientation();
                _sensorOrientationChecker.unregisterOrientationListener();
                _sensorOrientationChecker.onPause();
                captureWithOrientation(options, promise, deviceOrientation);
            }
        });
    } else {
        captureWithOrientation(options, promise, orientation);
    }
}

private void captureWithOrientation(final ReadableMap options, final Promise promise, int deviceOrientation) {
    Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
    if (null == camera) {
        promise.reject("No camera found.");
        return;
    }

    if (options.getInt("mode") == RCT_CAMERA_CAPTURE_MODE_VIDEO) {
        record(options, promise);
        return;
    }

    RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));

    if (options.hasKey("playSoundOnCapture") && options.getBoolean("playSoundOnCapture")) {
        MediaActionSound sound = new MediaActionSound();
        sound.play(MediaActionSound.SHUTTER_CLICK);
    }

    if (options.hasKey("quality")) {
        RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality"));
    }

    final Boolean shouldMirror = options.hasKey("mirrorImage") && options.getBoolean("mirrorImage");

    RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation);
    camera.takePicture(null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            if (shouldMirror) {
                data = mirrorImage(data);
                if (data == null) {
                    promise.reject("Error mirroring image");
                }
            }

            camera.stopPreview();
            camera.startPreview();
            WritableMap response = new WritableNativeMap();
            switch (options.getInt("target")) {
                case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
                    String encoded = Base64.encodeToString(data, Base64.DEFAULT);
                    response.putString("data", encoded);
                    promise.resolve(response);
                    break;
                case RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL: {
                    File cameraRollFile = getOutputCameraRollFile(MEDIA_TYPE_IMAGE);
                    if (cameraRollFile == null) {
                        promise.reject("Error creating media file.");
                        return;
                    }

                    Throwable error = writeDataToFile(data, cameraRollFile);
                    if (error != null) {
                        promise.reject(error);
                        return;
                    }

                    addToMediaStore(cameraRollFile.getAbsolutePath());
                    response.putString("path", Uri.fromFile(cameraRollFile).toString());
                    promise.resolve(response);
                    break;
                }
                case RCT_CAMERA_CAPTURE_TARGET_DISK: {
                    File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                    if (pictureFile == null) {
                        promise.reject("Error creating media file.");
                        return;
                    }

                    Throwable error = writeDataToFile(data, pictureFile);
                    if (error != null) {
                        promise.reject(error);
                        return;
                    }

                    addToMediaStore(pictureFile.getAbsolutePath());
                    response.putString("path", Uri.fromFile(pictureFile).toString());
                    promise.resolve(response);
                    break;
                }
                case RCT_CAMERA_CAPTURE_TARGET_TEMP: {
                    File tempFile = getTempMediaFile(MEDIA_TYPE_IMAGE);
                    if (tempFile == null) {
                        promise.reject("Error creating media file.");
                        return;
                    }

                    Throwable error = writeDataToFile(data, tempFile);
                    if (error != null) {
                        promise.reject(error);
                    }

                    response.putString("path", Uri.fromFile(tempFile).toString());
                    promise.resolve(response);
                    break;
                }
            }
        }
    });
}

但是,我查看了 iOS 端,发现它们确实在metadata那里处理了该对象。

编辑

此外,该master分支仍然不处理该metadata选项。见这里。似乎您必须指定一个特定的target并且取决于它是什么,它会返回一个特定的数据类型,但不幸的是与位置无关。

于 2016-09-13T23:53:15.843 回答