我使用Camera 1 API构建了一个自定义相机,由于某种原因,它会产生非常暗的图片(仅在前置摄像头上,后置摄像头效果很好)。摄影机预览会以正确的亮度显示摄影机,它只有在捕获图像并将其解码为位图后,它才会真正变暗。 我疯狂地搜索了一段时间,发现该问题报告了好几次,但找不到有效的解决方案。我使用的设备是Samsung J5。
CameraPreview:
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
上(拍摄图像时),我将字节发送到此方法,该方法将字节解码为位图,将其放入捆绑包中,然后传递给下一个片段:
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()中,将曝光补偿设置为最大值,但这似乎没有效果。感谢任何帮助。
编辑: 事实证明,添加延迟并没有什么不同,所以结果如下(相机预览与实际拍摄的图像):
单击捕获按钮时,通过调用mCamera.takePicture(null, null, pictureCallback)
捕获图像(回调仅将字节传输到上述方法)。
答案 0 :(得分:1)
经过所有的流血,汗水和眼泪,我找到了解决方案。我注意到预览和最终图片看起来不像是相同的分辨率(您可以看到预览图片中的瓶子比捕获的图片更宽)。所以我试图使它们相同或尽可能接近。我在startPreview()
中的surfaceCreated()
之后调用此方法:
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;
}
现在,我的图片和预览非常适合我的两台设备,并且亮度和分辨率都相同。
答案 1 :(得分:0)
由于我使用自定义相机,并且遇到了许多与不同设备屏幕尺寸等相关的问题,因此这对我来说是唯一有效的方法,因此,我建议使用以下库来帮助您解决问题。它也有很多选择。
答案 2 :(得分:0)
我来晚了一点,但是我遇到了同样的问题,并且通过非常小的更新i-e进行了修复
parameters.setPreviewSize(getWidth(),getHeight());
parameters.setPictureSize(getWidth(),getHeight());
parameters.setAutoWhiteBalanceLock(false); // the main life-saver lock ever :)
camera.setParameters(parameters);
width
和hight
可以是任何值,但是代码中启用了自动白平衡锁定。