gpt4 book ai didi

android - SurfaceView 相机预览代码在 Pre Lollipop 上运行良好但在 Lollipop 设备上崩溃

转载 作者:行者123 更新时间:2023-11-29 01:26:37 31 4
gpt4 key购买 nike

我实现了一个自定义的 SurfaceView 来绘制相机预览,并在其上完成了所有捕获和手动对焦操作。它在 Pre-Lolipop 设备上运行良好,但问题是它在 Lollipop 设备上崩溃。 发生的最奇怪的事情是,应用程序抛出 ANR,当我单击“确定”时,应用程序从我离开它的地方开始运行。
LogCat 显示“在跳过 ANR 后无法连接到相机错误,我可以轻松连接甚至操纵我的相机”。是因为我使用了已弃用的 Camera API 而不是 Camera2 和 Lollipop 设备吗??

我的代码是这样的:

public class CameraPreview
implements
SurfaceHolder.Callback {
private int cameratype=Camera.CameraInfo.CAMERA_FACING_BACK;
private Camera mCamera = null;
public Camera.Parameters params;
private SurfaceHolder sHolder;
private String TAG="CameraPreview";
public List<Camera.Size> supportedSizes;

public int isCamOpen = 0;
public boolean isSizeSupported = false;
private int previewWidth, previewHeight;
private List<String> mSupportedFlashModes;
private boolean flashon=false;
private final static String MYTAG = "CameraPreview";
private ProgressDialog loading;

public CameraPreview(int width, int height) {
Log.i("campreview", "Width = " + String.valueOf(width));
Log.i("campreview", "Height = " + String.valueOf(height));
previewWidth = width;
previewHeight = height;
}

private int openCamera() {
if (isCamOpen == 1) {
releaseCamera();
}

mCamera = Camera.open(cameratype);

if (mCamera == null) {
return -1;
}

if (TouchActivity.reference.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
mCamera.setDisplayOrientation(90);
} else {
mCamera.setDisplayOrientation(0);
}

params = mCamera.getParameters();
params.setPreviewSize(previewWidth, previewHeight);

try {
mCamera.setParameters(params);
} catch (RuntimeException e) {
e.printStackTrace();
}

mCamera.startPreview();
try {
mCamera.setPreviewDisplay(sHolder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
return -1;
}
isCamOpen = 1;
return isCamOpen;
}
public int isCamOpen() {
return isCamOpen;
}

public void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
isCamOpen = 0;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
sHolder = holder;
isCamOpen = openCamera();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {

releaseCamera();

}

/**
* Called from PreviewSurfaceView to set touch focus.
*
* @param - Rect - new area for auto focus
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void doTouchFocus(final Rect tfocusRect) {
Log.i(TAG, "TouchFocus");
try {
final List<Camera.Area> focusList = new ArrayList<Camera.Area>();
Camera.Area focusArea = new Camera.Area(tfocusRect, 1000);
focusList.add(focusArea);

Camera.Parameters para = mCamera.getParameters();
para.setFocusAreas(focusList);
para.setMeteringAreas(focusList);
mCamera.setParameters(para);

mCamera.autoFocus(myAutoFocusCallback);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Unable to autofocus");
}

}

/**
* AutoFocus callback
*/
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){

@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
if (arg0){
mCamera.cancelAutoFocus();
}
}
};




public void capturePicture(){
mCamera.takePicture(null, null, mPicture);


}

private File getOutputMediaFile(){

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "UltimateCameraGuideApp");

if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("Camera Guide", "Required media storage does not exist");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");

//DialogHelper.showDialog("Success!", "Your picture has been saved!", TouchActivity.reference.getActivity());

return mediaFile;
}

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

//This One is Just for Getting a File Named after it
loading=new ProgressDialog(BaseImagesContainer.reference);
loading.setMessage("Getting Image Ready");
loading.show();
File pictureFile =getOutputMediaFile();
if (pictureFile == null){
Toast.makeText(TouchActivity.reference.getActivity(), "Image retrieval failed.", Toast.LENGTH_SHORT)
.show();
return;
}
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
if(cameratype==Camera.CameraInfo.CAMERA_FACING_BACK){
bmp=rotateImage(90,bmp);
}else{
bmp=rotateImage(270,bmp);

}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,1, stream);
byte[] flippedImageByteArray = stream.toByteArray();
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(flippedImageByteArray);
fos.close();
// Restart the camera preview.
//safeCameraOpenInView(mCameraView);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Uri destination = Uri.fromFile(new File(TouchActivity.reference.getActivity().getCacheDir(), "cropped"));
Uri source = Uri.fromFile(new File(pictureFile.getPath()));
Crop.of(source, destination).withMaxSize(800,800).start(TouchActivity.reference.getActivity());
}
};


public Bitmap rotateImage(int angle, Bitmap bitmapSrc) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}


public void switchCamera(){

mCamera.stopPreview();
//NB: if you don't release the current camera before switching, you app will crash
mCamera.release();

//swap the id of the camera to be used
if(cameratype==Camera.CameraInfo.CAMERA_FACING_BACK){
cameratype=Camera.CameraInfo.CAMERA_FACING_FRONT;
}else{
cameratype=Camera.CameraInfo.CAMERA_FACING_BACK;
}
try{
mCamera = Camera.open(cameratype);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(TouchActivity.reference.getActivity(),"Can't Open the Camera",Toast.LENGTH_LONG).show();
}

if (TouchActivity.reference.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {

mCamera.setDisplayOrientation(90);

} else {

mCamera.setDisplayOrientation(0);

}

try{
mCamera.setPreviewDisplay(sHolder);
mCamera.startPreview();
}
catch(Exception e){
e.printStackTrace();
}

}

public void switchflash(){
//Do the On Flash for now
if(!flashon){
mSupportedFlashModes = mCamera.getParameters().getSupportedFlashModes();
if (mSupportedFlashModes != null && mSupportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)){
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
mCamera.setParameters(parameters);
}
}else{
//flash on
//do teh off now
Camera.Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
}
flashon=!flashon;

}

public void stopLoading(){
loading.dismiss();
//DialogHelper.showDialog("Oops!", "Your crop had been cancelled !", TouchActivity.reference.getActivity());

}

最佳答案

根本原因与 Camera Preview appearing really slow in Android 中的相同。我什至在我的 comment 中提到过 ANR那里。

为避免 ANR,您必须将 Camera.open() 卸载到另一个线程。如果您投资在处理程序线程上执行此操作,如所述here ,您的 onPictureTaken() 回调也不会导致 ANR。

您需要将此回调的某些部分包装在 runOnUiThread() 中,但这肯定会有所返回。

关于android - SurfaceView 相机预览代码在 Pre Lollipop 上运行良好但在 Lollipop 设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658402/

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