在我的应用程序中,我使用服务自动捕获图像。
从我的服务中,我正在打开相机活动。
相机活动
public class CameraActivity extends Activity {
private static final String TAG = "CameraDemo";
SharedPreferences pref;
CameraPreview preview;
Button click;
// enter email adress from where the email will be sent
String fromPassWord;
int cameraType;
private static final int FRONT_CAMERA = 0;
private static final int BACK_CAMERA = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
cameraType = getIntent().getIntExtra("cameraType",BACK_CAMERA);
preview = new CameraPreview(this,cameraType);
((FrameLayout) findViewById(com.abc.xyz.R.id.preview)).addView(preview);
handler.sendEmptyMessageDelayed(0, 100);
Log.d(TAG, "onCreate'd");
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
try{
if(preview.camera != null ){
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}else{
finish();
}
}catch (Exception e) {
// TODO: handle exception
finish();
}
};
};
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d(TAG, "onShutter'd");
Log.v(TAG, "onShutter'd");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
Log.v(TAG, "onPictureTaken - raw");
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try {
//my method for storing image
StoreByteImage(data, 75, "ImageName");
Log.v("on picture taken", "picture callback");
} catch (Exception e) {
Log.d(TAG, "Exception--------------------1");
e.printStackTrace();
}
finish();
}
};
}
CameraPreview.java
class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
int cameraType;
Context _context;
private static final int FRONT_CAMERA = 0;
private static final int BACK_CAMERA = 1;
//SurfaceHolder controls the surface size and format, edit the pixels in the surface, and monitor changes to the surface
CameraPreview(Context context, int type){
super(context);
//Return the SurfaceHolder providing access and control over this SurfaceView's underlying surface
mHolder = getHolder();
//Add a callback interface to this holder
mHolder.addCallback(this);
//Set the surface type
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.cameraType = type;
_context = context;
}
//Create a surface
public void surfaceCreated(SurfaceHolder sHolder){
if(cameraType == FRONT_CAMERA){
try{
camera = openFrontFacingCameraGingerbread(); //method to open camera...
Log.v("cameraFornt", "value"+camera);
}catch (Exception e) {
e.printStackTrace();
}
}
}else{
//do nothing
}
if(camera == null)
return;
try{
camera.setPreviewDisplay(sHolder);
//Installs a callback to be invoked for every preview frame in addition to displaying them on the screen
camera.setOneShotPreviewCallback(new PreviewCallback(){
//Called as preview frames are displayed
public void onPreviewFrame(byte[] data, Camera arg1){
CameraPreview.this.invalidate();
}
});
} catch (Exception e){
e.printStackTrace();
}
}
//called immediately before a surface is being destroyed
public void surfaceDestroyed(SurfaceHolder sHolder){
if(camera == null){
return;
}
Log.v("Surface dESTroyed ","Camera Preview");
camera.release();
}
Log.v("Camera",""+camera);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
Log.v("SurfaceChangesCalled", "true");
if(camera == null){
return;
}
}
public void draw(Canvas canvas){
super.draw(canvas);
//The Paint class holds the style and color information about how to draw geometries, text and bitmaps
Paint paint = new Paint(Color.RED);
Log.d(TAG, "draw");
Log.v("Draw", "Canvas");
//Draw the text, with origin at (canvas.getWidth()/2, canvas.getHeight()/2), using the specified paint
canvas.drawText("Preview", canvas.getWidth() / 2, canvas.getHeight() / 2, paint);
}
}
现在我的问题是当我的主应用程序打开并且相机活动开始时我得到了ANR。它冻结在线camera.release();
但是当我的主应用程序没有打开时,它就像一个魅力。
我也尝试在CameraActivity的onPause()方法中释放相机,但没有运气。