gpt4 book ai didi

java - 相机预览很好,但前置相机产生的照片非常暗

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

我使用 Camera 1 API 构建了一个自定义相机,由于某种原因,它生成了非常暗的图片(仅在前置摄像头上,后置摄像头工作得很好)。相机预览以正确的亮度显示相机应有的样子 - 只有当图像被捕获并解码为位图时,它才会看起来真的暗。我疯狂地用谷歌搜索了一段时间,发现这个问题被报告了好几次,但找不到有效的解决方案。我使用的设备是三星 J5。

相机预览:

class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private static final String CAMERA = "CAMERA";

private static Camera mCamera;

private final CameraActivity cameraActivity;
private final SurfaceHolder mHolder;

public CameraPreview(Camera camera, CameraActivity cameraActivity) {
super(cameraActivity);
this.cameraActivity = cameraActivity;
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void setCameraDisplayOrientation(int cameraId) {
Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
final int rotation = cameraActivity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;

switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}

int result;
if (info.facing == cameraId) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
} else {
result = (info.orientation - degrees + 360) % 360;
}

mCamera.setDisplayOrientation(result);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
cameraActivity.isSafeToTakePicture(true);
Camera.Parameters params = mCamera.getParameters();
// my attempt at preventing darkness
params.setExposureCompensation(params.getMaxExposureCompensation());
if(params.isAutoExposureLockSupported()) {
params.setAutoExposureLock(false);
}

mCamera.setParameters(params);
} catch (IOException e) {
Log.d(CAMERA, "An error occured when setting up the camera preview " + e.getMessage());
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
}
}

在我的 CameraPictureCallback 上(拍摄图像时),我将字节发送到此方法,该方法将字节解码为位图,将其放入一个包中并将其传递给下一个 fragment :

 public void openFragmentWithBitmap(byte[] bytes) {
final BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, scalingOptions);
final Bundle bundle = new Bundle();
bundle.putParcelable(SELFIE, bitmap);

mCamera.stopPreview();
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
final Fragment startChainFragment = new StartChainFragment();
startChainFragment.setArguments(bundle);
ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down)
.replace(R.id.rlPlaceholder, startChainFragment, StartChainFragment.TAG)
.addToBackStack(null)
.commit();
}

我是不是漏掉了什么技巧?在我的 surfaceCreated() 中,我将曝光补偿设置为最大值,但这似乎没有效果。感谢任何帮助。

编辑:事实证明,添加延迟并没有什么不同,所以这里是结果(相机预览与实际拍摄的图像):

相机预览: enter image description here

捕获的图像: enter image description here

当点击捕获按钮时,通过调用 mCamera.takePicture(null, null, pictureCallback) 捕获图像(回调只是将字节传输到上述方法)。

最佳答案

在经历了所有的鲜血、汗水和泪水之后,我找到了解决方案。我注意到预览图片和最终图片看起来分辨率不同(您可以看到预览图片中的瓶子比捕获的图片宽)。所以我试图让它们相同或尽可能接近。我在 surfaceCreated() 中的 startPreview() 之后调用此方法:

private void loadCameraParameters() {
final Camera.Parameters camParams = mCamera.getParameters();

final Camera.Size previewSize = getOptimalPreviewSize(camParams.getSupportedPreviewSizes(), screenWidth, screenHeight);
camParams.setPreviewSize(previewSize.width, previewSize.height);

final Camera.Size pictureSize = getOptimalPreviewSize(camParams.getSupportedPictureSizes(), screenWidth, screenHeight);
camParams.setPictureSize(pictureSize.width, pictureSize.height);

mCamera.setParameters(camParams);
}

其中 getOptimalPreviewSize() 是:

private Camera.Size getOptimalPreviewSize(List<Size> sizes, int targetWidth, int targetHeight) {
final double aspectTolerance = 0.05;
final double targetRatio = (double) targetWidth/targetHeight;

if (sizes == null) {
return null;
}

Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;

for (Camera.Size size : sizes) {
final double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > aspectTolerance) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}

if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}

return optimalSize;
}

现在我的图片和预览在我的两台设备上都完美匹配,具有相同的亮度分辨率。

关于java - 相机预览很好,但前置相机产生的照片非常暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53584614/

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