gpt4 book ai didi

android - 使用 Google Vision 条码阅读器访问 AutoFocus/Flash

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:32 26 4
gpt4 key购买 nike

我在这里玩条形码扫描器的原始示例:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/BarcodeCaptureActivity.java

他们可以像这样在相机工厂内启动自动对焦/闪光灯:

    // Creates and starts the camera.  Note that this uses a higher resolution in comparison
// to other detection examples to enable the barcode detector to detect small barcodes
// at long distances.
CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(1600, 1024)
.setRequestedFps(15.0f);

// make sure that auto focus is an available option
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
builder = builder.setFocusMode(
autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
}

mCameraSource = builder
.setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
.build();

但是,当前版本中 cameraSource 构建器上的此方法已不存在,因此无法访问此设置。此外,我需要在使用过程中更改 FlashMode,所以这也不是这样做的方法。我发现了这个丑陋的访问相机的解决方案:

public static Camera getCamera(@NonNull CameraSource cameraSource) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();

for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
Camera camera = (Camera) field.get(cameraSource);
if (camera != null) {
return camera;
}

return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
}

break;
}
}

return null;
}

虽然有效,但无济于事:调用 getParameters().setFocusMode() 时出现此异常:

Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference

显然我正在做的不是正确的方法,但似乎没有关于它的文档。

感谢提示。

最佳答案

只需如下优化您的代码,您必须在构建 Camera Source 类后调用此方法。

private Camera camera = null;
boolean flashmode=false;
private void flashOnButton() {
camera=getCamera(mCameraSource);
if (camera != null) {
try {
Camera.Parameters param = camera.getParameters();
param.setFlashMode(!flashmode?Camera.Parameters.FLASH_MODE_TORCH :Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
flashmode = !flashmode;
if(flashmode){
showToast("Flash Switched ON");
}
else {
showToast("Flash Switched Off");
}
} catch (Exception e) {
e.printStackTrace();
}

}
}
private static Camera getCamera(@NonNull CameraSource cameraSource) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();

for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
Camera camera = (Camera) field.get(cameraSource);
if (camera != null) {
return camera;
}
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
return null;
}

这将帮助您使用 camerasource 对象在 Google Vision Api 中启用闪光灯

关于android - 使用 Google Vision 条码阅读器访问 AutoFocus/Flash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com