我有一个自定义相机,在某些设备中可以正常工作。它在三星Galaxy Gran Duos(三星gt i9082,安卓4.2.2)上运行良好,但当我试图拍摄之前放大的图像时,它会冻结,不会崩溃,唯一的方法就是按下后退按钮。这种情况只发生在三星GalaxyGranDuos。
我用来拍照的代码:
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
我认为您需要通过使用camera.isZoomSupported()
确认您的相机支持变焦,然后如果支持,则需要使用camera.cancelAutoFocus()
取消自动对焦以防止图像失真。但只有当你的设备真的支持缩放时,这才有效。如果没有,您需要捕获保持图像,然后使用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());
}
}
};
这是特定类型内核的一些问题。
试试这种方法。
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;
}