我在我的项目中使用OpenCV4Android,我正在使用JavaCameraView。我试图切换手机闪光灯,但失败了。这是我的尝试。
public class Camera_View extends JavaCameraView {
private static final String TAG = "Camera View";
public boolean isFlashOn = false;
public Camera_View(Context context, AttributeSet attrs) {
super(context, attrs);
}
public List<String> getEffectList() {
return mCamera.getParameters().getSupportedColorEffects();
}
public boolean isEffectSupported() {
return (mCamera.getParameters().getColorEffect() != null);
}
public String getEffect() {
return mCamera.getParameters().getColorEffect();
}
public void setEffect(String effect) {
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(effect);
mCamera.setParameters(params);
}
public List<Size> getResolutionList() {
return mCamera.getParameters().getSupportedPreviewSizes();
}
public void setResolution(Size resolution) {
disconnectCamera();
mMaxHeight = resolution.height;
mMaxWidth = resolution.width;
connectCamera(getWidth(), getHeight());
}
public Size getResolution() {
return mCamera.getParameters().getPreviewSize();
}
public void switchFlash(boolean flash_on) {
Parameters p = mCamera.getParameters();
if (!flash_on)
p.setFlashMode(Parameters.FLASH_MODE_OFF);
else
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(p);
}
public void switchFlash() {
Camera tcamera = null;
tcamera = Camera.open();
Parameters p = tcamera.getParameters();
if (isFlashOn)
p.setFlashMode(Parameters.FLASH_MODE_OFF);
else
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
isFlashOn = !isFlashOn;
tcamera.setParameters(p);
}
public void setupCameraFlashLight(boolean flash_on) {
Camera camera = mCamera;
if (camera != null) {
Parameters params = camera.getParameters();
if (params != null) {
if (!flash_on) {
isFlashOn = false;
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.startPreview();
} else {
isFlashOn = true;
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
}
}
}
我尝试使用三种方法打开或关闭闪光灯。但似乎喜欢我在网上找到的nativeCameraView,但我使用的是JavaCameraView。当我尝试运行编码时,我得到了java.lang.NullPointerException。
任何人都可以帮助我吗?