如何使用React-Native-Camera捕获获取元数据?

时间:2016-09-13 08:34:50

标签: android react-native react-native-camera

我无法弄清楚如何使用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));

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

1 个答案:

答案 0 :(得分: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手机上测试它)并搜索本机{{1函数和那里没有任何处理capture选项的东西,这很难过。尝试搜索metadata,但您找不到任何内容:

metadata

但是,我查看了iOS端,看到他们确实处理了那里的@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; } } } }); } 对象。

修改

此外,metadata分支仍然无法处理master选项。见here。您似乎必须指定一个特定的metadata,并且根据具体情况,它会返回一个特定的数据类型,但遗憾的是没有任何与位置相关的内容。