我实现了一个自定义SurfaceView来绘制相机预览,并完成捕获和手动聚焦的所有操作。它在Pre-Lolipop设备上运行良好,但问题是它在Lollipop设备上崩溃了。
发生的最奇怪的事情是,应用程序抛出一个ANR,当我单击Ok时,应用程序从我离开它的位置开始运行。
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());
}
答案 0 :(得分:1)
根本原因与 Camera Preview appearing really slow in Android 相同。我甚至在comment那里提到了ANR。
要避免ANR,您必须将 Camera.open()卸载到另一个线程。如果您在Handler线程上投资,如here所述,您的 onPictureTaken()回调也不会导致ANR。
您需要在runOnUiThread()
中包含此回调的某些部分,但这肯定会有所回报。