8

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

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

他们可以像这样在相机工厂内启动 AutoFocus/Flash:

    // 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 builder上的这个方法在当前版本中已经消失了,所以这个设置不能被访问。另外我需要在使用过程中更改 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

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

感谢您的提示。

4

3 回答 3

16

只需按以下方式优化您的代码,您必须在构建 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 中启用 flash 。

于 2016-12-01T10:57:57.283 回答
0

这是一个有效的 kotlin 变体:

private var flashmode: Boolean = false
private var camera: Camera? = null

private fun flashOnButton() {
    camera = getCamera(cameraSource)

    if (camera != null) {
        try {
            val param = camera!!.parameters

            param.setFlashMode(if (!flashmode) Camera.Parameters.FLASH_MODE_TORCH else Camera.Parameters.FLASH_MODE_OFF)
            camera?.parameters = param
            flashmode = !flashmode
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }
}

private fun getCamera(cameraSource: CameraSource): Camera? {
    val declaredFields = CameraSource::class.java.declaredFields

    for (field in declaredFields) {
        if (field.type === Camera::class.java) {
            field.isAccessible = true

            try {
                val camera = field.get(cameraSource) as Camera
                return if (camera != null) {
                    camera
                } else null
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            }

            break
        }
    }

    return null
}

该解决方案目前有效。但我不知道如何处理这个,android.hardware.Camera将来不再支持。也许没有更好的解决方案android.hardware.Camera

于 2020-09-16T13:47:57.550 回答
-1

直接使用 CameraSource 即可。

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

您将能够使用闪光灯/对焦。

CameraSource
.setFlashMode()
.setFocusMode()
于 2018-04-04T12:28:43.093 回答