Android自定义相机缩放后冻结

我有一个自定义相机在某些设备上工作正常。 它适用于三星Galaxy Gran Duos(三星-gt i9082,Android 4.2.2)但是当我尝试捕捉图像时,我之前放大了,它冻结,没有崩溃,唯一的出路就是按下返回键。 这种情况只发生在三星Galaxy Gran Duos上。

我用来拍照的代码:

Camera.PictureCallback photoCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { try { } catch (Exception e) { if (flePicture== null){ Log.d("camera", "Error creating media file, check storage permissions: " + e.getMessage()); return; } } try { FileOutputStream fos = new FileOutputStream(flePicture); fos.write(data); fos.close(); } catch (FileNotFoundException e) { Log.d("camera", "File not found: " + e.getMessage()); } catch (IOException e) { Log.d("camera", "Error accessing file: " + e.getMessage()); } } }; 

以及用于放大的代码:

 private void zoomIn() { if (pblnInPreview) { Camera.Parameters parameters = camCamera.getParameters(); if ((parameters.getZoom() + 1) < parameters.getMaxZoom()) { parameters.setZoom(parameters.getZoom() + 1); camCamera.setParameters(parameters); } } } 

logcat的:

 04-07 17:21:14.386: E/BrcmCamera(130): processControlBuffer: Corrupt stream error raised by camera - sensor communication failure 

Solutions Collecting From Web of "Android自定义相机缩放后冻结"

我认为您需要确认您的相机支持使用camera.isZoomSupported()进行缩放,然后如果支持您需要使用camera.isZoomSupported()取消自动对焦以防止图像失真。 但这仅在您的设备实际支持缩放时才有效。 如果不是,您需要捕获保持图像并使用Bitmap.createBitmap放大后Bitmap.createBitmap的部分。

这不是强烈相关的答案。 我知道。

但我想说三星4.2.2有很多缺陷问题。 它有ClipboardManager崩溃,ActionBar AppCompat崩溃等等。

只需使用简单的if子句过滤它,就可以节省生命。

 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1 && Build.MANUFACTURER.toUpperCase().contains("SAMSUNG")){ // ignore }else{ // your logic on the go } 

你拍完照片后再次开始拍照吗?

添加此camera.startPreview();

尝试这个

  Camera.PictureCallback photoCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { camera.startPreview(); try { } catch (Exception e) { if (flePicture== null){ Log.d("camera", "Error creating media file, check storage permissions: " + e.getMessage()); return; } } try { FileOutputStream fos = new FileOutputStream(flePicture); fos.write(data); fos.close(); } catch (FileNotFoundException e) { Log.d("camera", "File not found: " + e.getMessage()); } catch (IOException e) { Log.d("camera", "Error accessing file: " + e.getMessage()); } } }; 

这是特定types内核的一些问题。

试试这种方法。

 private static final String TAG = Test.class.getSimpleName(); private boolean isPreviewStarted; private Camera camera; Camera.PictureCallback photoCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { // check if this needs to stop the preview if (deviceNeedsStopPreviewToShoot()) { stopPreview(); } // save your image // restart preview if needed. startPreview(); } }; public void startPreview() { if (!isPreviewStarted && camera != null) { camera.startPreview(); isPreviewStarted = true; } } public void stopPreview() { if (isPreviewStarted && camera != null) { camera.stopPreview(); isPreviewStarted = false; } } public static boolean deviceNeedsStopPreviewToShoot() { String[] oldDevices = {"smdk4210", "aries"}; boolean needs = Arrays.asList(oldDevices).contains(Build.BOARD); Log.e(TAG, "Device " + Build.BOARD + (needs ? " needs " : " doesn't need ") + "to stop preview"); return needs; }