如何使用 React-Native-Camera 捕获获取元数据?
How do I get metadata using React-Native-Camera capture?
我不知道如何使用 react-native-camera 捕获方法获取捕获图像的 元数据。
我假设它是这样的:
this.camera.capture({metadata: true})
.then((data) => console.log(data))
.catch(error => alert("error: " + error));
但它只是 returns 路径,所以也是:
this.camera.capture({metadata: 'location'})
.then((data) => console.log(data))
.catch(error => alert("error: " + error));
我不清楚文档元数据的用法。
传递元数据参数的正确方法是什么?
有人让这个工作吗?
这可能是在黑暗中拍摄,但如果您阅读 README.md https://github.com/lwansbrough/react-native-camera 的第一段,它会说:
These docs are for the work in progress v1 release. If you want to use the latest and greatest and can deal with significant instability you can install with 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 phone) 并搜索本机 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
选项。参见 here。看起来你必须指定一个特定的 target
并且根据它是什么,它将 return 你一个特定的数据类型,但不幸的是与位置无关。
我不知道如何使用 react-native-camera 捕获方法获取捕获图像的 元数据。
我假设它是这样的:
this.camera.capture({metadata: true})
.then((data) => console.log(data))
.catch(error => alert("error: " + error));
但它只是 returns 路径,所以也是:
this.camera.capture({metadata: 'location'})
.then((data) => console.log(data))
.catch(error => alert("error: " + error));
我不清楚文档元数据的用法。 传递元数据参数的正确方法是什么? 有人让这个工作吗?
这可能是在黑暗中拍摄,但如果您阅读 README.md https://github.com/lwansbrough/react-native-camera 的第一段,它会说:
These docs are for the work in progress v1 release. If you want to use the latest and greatest and can deal with significant instability you can install with 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 phone) 并搜索本机 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
选项。参见 here。看起来你必须指定一个特定的 target
并且根据它是什么,它将 return 你一个特定的数据类型,但不幸的是与位置无关。