拍摄照片后,再次尝试运行应用程序时出现黑屏,没有预览,如果我想运行相机或使用相机的任何其他应用程序,则会出现相机被锁定的消息! 任何提示或想法为什么?
提前致谢。
public class Capture extends Activity {
private Camera mCamera=null;
private ViewCamera mPreview=null;
public static final int MEDIA_TYPE_IMAGE = 1;
Mat jpegData;
Mat bgrMat;
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.i("", "OpenCV loaded successfully");
// Create and set View
// setContentView(R.layout.camera);
jpegData = new Mat();
bgrMat = new Mat();
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
// Create an instance of Camera
Log.i("", "Trying to load OpenCV library");
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this,
mOpenCVCallBack)) {
Log.e("", "Cannot connect to OpenCV Manager");
}
mCamera = getCameraInstance();
Log.d("", "getCameraInstance *");
// Create our Preview view and set it as the content of our activity.
mPreview = new ViewCamera(this, mCamera);
Log.d("", "new view * ");
FrameLayout preview = (FrameLayout) findViewById(R.id.frame);
Log.d("", "frame layout * ");
preview.addView(mPreview);
Log.d("", "addview * ");
}
/** A safe way to get an instance of the Camera object. */
public Camera getCameraInstance() {
if(this.mCamera==null)
try {
this.mCamera = Camera.open(); // attempt to get a Camera
// instance
Log.d("", " open camera * ");
// c.lock();
} catch (Exception e) {
// Camera is not available (in use or does not exist)
Log.d("", "can't open camera ");
}
return this.mCamera; // returns null if camera is unavailable
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("", "on picture taken starts ");
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
Log.d("", "file get media file ");
if (pictureFile == null) {
Log.d("",
"Error creating media file, check storage permissions ");
return;
}
try {
Log.d("", "File try save ");
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(byteArray);
fos.close();
Log.d("", "File saved ");
} catch (FileNotFoundException e) {
Log.d("", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("", "Error accessing file: " + e.getMessage());
}
mCamera.startPreview();
Log.d("", "start preview after saving * ");
}
};
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.d("", "on touch starts * ");
System.gc();
mCamera.stopPreview();
mCamera.takePicture(shutterCallback, null, mPicture);
Log.d("", "taken pic * ");
return super.onTouchEvent(event);
}
private final ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d("", "start shutter * ");
/* AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);*/
Log.d("", "end shutter * ");
}
};
}
表面视图
public class ViewCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public ViewCamera(Context context, Camera camera) {
super(context);
Log.d("", "start view camera constructor * ");
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
Log.d("", "view camera constructor holder and callback * ");
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
if (mCamera != null) {
Log.d("", "surface created camera !=null * ");
Parameters params = mCamera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
Log.d("", "camera orientation not landscape ");
params.set("orientation", "portrait"); //
mCamera.setDisplayOrientation(90);
}
//mCamera.setParameters(params);
Log.d("", "surface created start preview * ");
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
} catch (IOException e) {
Log.d("", "Error setting camera preview: " + e.getMessage());
}
}
// check if device has camera
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
Log.d("", "there is a camera ");
return true;
} else {
// no camera on this device
Log.d("", "there is no camera ");
return false;
}
}
/*
* private Camera.Size getBestPreviewSize(int width, int height,
* Camera.Parameters parameters) { Camera.Size result = null;
* Log.d("", "start getbestsize * ");
*
* for (Camera.Size size : parameters.getSupportedPreviewSizes()) { if
* (size.width <= width && size.height <= height) { if (result == null) {
* result = size; } else { int resultArea = result.width * result.height;
* int newArea = size.width * size.height;
*
* if (newArea > resultArea) { result = size; } } } }
*
* return (result); }
*/
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
// empty. Take care of releasing the Camera preview in your activity.
Log.d("", "surface destroy starts * ");
mCamera.setPreviewCallback(null);
Log.d("", "call back null * ");
this.getHolder().removeCallback(this);
Log.d("", "remove call back * ");
if (mCamera != null) {
Log.d("", "camera not null * ");
mCamera.stopPreview();
Log.d("", "preview stopped * ");
mCamera.release();
Log.d("", "release camera * ");
mCamera = null;
Log.d("", "camera null * ");
Log.d("surfaceDestroyed", "surfaceDestroyed");
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// TODO Auto-generated method stub
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
Log.d("", "surface changed starts * ");
if (mHolder.getSurface() == null) {
// preview surface does not exist
Log.d("", "surface changed get surface=null * ");
return;
}
// stop preview before making changes
try {
Log.d("", "surface changed stop preview * ");
mCamera.stopPreview();
} catch (Exception e) {
}
// set preview size and make any resize, rotate or
// reformatting changes here
Log.d("", "surface changed parameters * ");
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
// Camera.Size size = getBestPreviewSize(w, h, parameters);
/*
* if (size != null) { parameters.setPreviewSize(size.width,
* size.height); mCamera.setParameters(parameters); }
*/
mCamera.setParameters(parameters);
// start preview with new settings
try {
Log.d("", "surface changedresume start preview * ");
// if (mCamera != null) {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
// }
} catch (Exception e) {
Log.d("",
"Error starting camera preview: " + e.getMessage());
}
}
}