我正在开发Android应用程序。我创建了一个自定义相机类来捕捉图像。它在很多设备上运行良好,但是当我用三星Galaxy S4尝试它时,它返回带有灰色lins的图像,如图所示。我的代码是:
![`*public void surfaceCreated(SurfaceHolder holder) {
frontCam = SharedUserPrefs.getSharedPrefData(AutoCapture.this,
Constants.IS_FRONT_CAMERA);
if (frontCam.equals(Constants.VALUE_ON)) {
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
mCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG,
"Camera failed to open: "
+ e.getLocalizedMessage());
}
}
}
} else {
mCamera = Camera.open();
}
mCamera.setDisplayOrientation(90);
Camera.Parameters p = mCamera.getParameters();
if (getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
if (flashOn.equals(Constants.VALUE_ON)) {
{
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
}
} else {
p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
p.setPictureFormat(ImageFormat.JPEG);
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
Camera.Size previewsize = previewSizes.get(0);
for (Camera.Size size : previewSizes) {
Log.d("Width---" + size.width, "Height---" + size.height);
}
p.setPreviewSize(previewsize.width, previewsize.height);
mCamera.setParameters(p);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
if (mPreviewRunning) {
mCamera.stopPreview();
}
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
System.out.println("Caught exception in surface chagned");
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
if (mPreviewRunning == false) {
if (mCamera != null) {
mPreviewRunning = false;
mCamera.stopPreview();
mCamera.release();
}
if (mCamera != null) {
mPreviewRunning = false;
mCamera.release();
}
}/*
* else if (mCamera != null) { mPreviewRunning = false;
* mCamera.stopPreview(); mCamera.release(); }
*/
}*`][2]
答案 0 :(得分:0)
我认为你有同样的问题我和其他人一样。
Samsung Galaxy S4 , Image gets corrupted while taking from camera