gpt4 book ai didi

android - Camera2 中的前置摄像头无法捕捉图像

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

我有来自 https://github.com/googlesamples/android-Camera2Basic 的 Camera2 代码.

我面临的问题是,当我转向前置摄像头时,我无法拍摄照片,但使用后置摄像头可以正常工作。

有没有人实现了 Camera2 Api,请帮忙!

这是代码 fragment :

private void setUpCameraOutputs(int width, int height) {
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : manager.getCameraIdList()) {
CameraCharacteristics characteristics
= manager.getCameraCharacteristics(cameraId);

// We don't use a front facing camera in this sample.
int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
Log.i(TAG,"Front Cam ID: "+ facing);
if (characteristics.get(CameraCharacteristics.LENS_FACING)==CameraCharacteristics.LENS_FACING_FRONT)
{



StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);


// For still image captures, we use the largest available size.
Size largest = Collections.max(
Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
new CompareSizesByArea());
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
ImageFormat.JPEG, /*maxImages*/2);
mImageReader.setOnImageAvailableListener(
mOnImageAvailableListener, mBackgroundHandler);

// Danger, W.R.! Attempting to use too large a preview size could exceed the camera
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of
// garbage capture data.
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
width, height, largest);

// We fit the aspect ratio of TextureView to the size of preview we picked.
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTextureView.setAspectRatio(
mPreviewSize.getWidth(), mPreviewSize.getHeight());
} else {
mTextureView.setAspectRatio(
mPreviewSize.getHeight(), mPreviewSize.getWidth());
}

mCameraId = cameraId;
return;
}
else
{
onActivityCreated(Bundle.EMPTY);
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
// Currently an NPE is thrown when the Camera2API is used but not supported on the
// device this code runs.
ErrorDialog.newInstance(getString(R.string.camera_error))
.show(getChildFragmentManager(), FRAGMENT_DIALOG);
}
}

最佳答案

问题是许多前置摄像头都有固定焦距。因此,在 lockFocus() 中的自动对焦触发器之后,自动对焦状态 ( CONTROL_AF_STATE ) 仍然是 INACTIVE 并且自动对焦触发器不执行任何操作。

因此,为了使其正常工作,您需要检查是否支持自动对焦。为此,将以下内容添加到 setUpCameraOutputs():

int[] afAvailableModes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);

if (afAvailableModes.length == 0 || (afAvailableModes.length == 1
&& afAvailableModes[0] == CameraMetadata.CONTROL_AF_MODE_OFF)) {
mAutoFocusSupported = false;
} else {
mAutoFocusSupported = true;
}

最后,拍照时不支持不锁焦:

private void takePicture() {
if (mAutoFocusSupported) {
lockFocus();
} else {
captureStillPicture();
}
}

关于android - Camera2 中的前置摄像头无法捕捉图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127258/

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