我正在尝试开发一个相机应用程序,它可以拍照并将其保存在 SQLite 数据库中。执行应用程序时出现很多错误。我的代码如下。任何想法?
CameraActivity.java
private Camera mCamera;
private CameraPreview mPreview;
private int CAMERA_RETURN_CODE=100;
private static final String TAG = "Take_Picture";
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Bitmap cameraBmp;
private int MAX_FACES = 1;
private Face[] faceList;
public RectF[] rects;
private Canvas canvas;
private Drawable pictureDataDrawable;
private MySQLiteHelper database;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_activity);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Create an instance of Camera
mCamera = getCameraInstance();
setCameraDisplayOrientation(this, 0, mCamera);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
database = new MySQLiteHelper(getApplicationContext());
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
private PictureCallback mPicture;
@Override
public void onClick(View v) {
//mCamera.startPreview();
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try{
if (data != null)
database.addEntry(data);
//mCamera.startPreview();
}
catch(Exception e){
Log.d(TAG, e.getMessage());
}
}
}
);
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(c.getNumberOfCameras()-1); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 360;
/*switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}*/
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
@Override
protected void onPause() {
super.onPause();
//releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
public void startFaceDetection(){
// Try starting Face Detection
Camera.Parameters params = mCamera.getParameters();
// start face detection only *after* preview has started
if (params.getMaxNumDetectedFaces() > 0){
// camera supports face detection, so can start it:
mCamera.startFaceDetection();
}
}
CameraPreview.java
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private String TAG;
private List<Size> mSupportedPreviewSizes;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
public void setCamera(Camera camera) {
if (mCamera == camera) { return; }
mCamera = camera;
if (mCamera != null) {
List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
mSupportedPreviewSizes = localSizes;
requestLayout();
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
/*
Important: Call startPreview() to start updating the preview surface. Preview must
be started before you can take a picture.
*/
mCamera.startPreview();
}
}
MySQLiteHelper.java
private static final int count = 0;
public static final String TABLE_IMAGE = "images";
public static final String COLUMN_ID = "_id";
public static final String PICTURE_DATA = "picture";
public static final String DATABASE_NAME = "images.db";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_CREATE = "create table "
+ TABLE_IMAGE + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + PICTURE_DATA
+ " blob not null);";
public static SQLiteDatabase database;
private static String TAG = "test";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public MySQLiteHelper(Context context, String name, CursorFactory factory,
int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_IMAGE);
onCreate(db);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void addEntry(byte [] array) throws SQLiteException{
ContentValues cv = new ContentValues();
//cv.put(KEY_NAME, name);
cv.put(PICTURE_DATA, array);
database.insert( TABLE_IMAGE, null, cv );
Log.w(TAG , "added " +count+ "images");
database.close();
}
错误
11-07 23:28:39.050: E/mm-libcamera2(176): PROFILE HAL: stopPreview(): E: 1383838119.067589459
11-07 23:28:39.050: E/mm-camera(201): config_MSG_ID_STOP_ACK: streamon_mask is not clear. Should not call PP_Release_HW
11-07 23:28:39.090: E/QCameraHWI(176): android::status_t android::QCameraHardwareInterface::setPreviewWindow(preview_stream_ops_t*):Received Setting NULL preview window
11-07 23:28:39.090: E/QCameraHWI(176): android::status_t android::QCameraHardwareInterface::setPreviewWindow(preview_stream_ops_t*): mPreviewWindow = 0x0x0, mStreamDisplay = 0x0xb8a9df90
11-07 23:28:39.090: E/mm-camera(201): config_shutdown_pp Camera not in streaming mode. Returning.
11-07 23:28:39.090: E/mm-camera(201): vfe_ops_deinit: E
11-07 23:28:39.120: E/qcom_sensors_hal(533): hal_process_report_ind: Bad item quality: 11
11-07 23:28:39.310: E/qcom_sensors_hal(533): hal_process_report_ind: Bad item quality: 11
11-07 23:28:39.330: E/mm-camera(201): sensor_load_chromatix: libchromatix_imx119_preview.so: 30
11-07 23:28:39.340: E/mm-camera(201): vfe_ops_init: E
11-07 23:28:39.360: E/mm-camera(201): vfe_legacy_stats_buffer_init: AEC_STATS_BUFNUM
11-07 23:28:39.360: E/mm-camera(201): vfe_legacy_stats_buffer_init: AEC_STATS_BUFNUM
11-07 23:28:39.360: E/mm-camera(201): mctl_init_stats_proc_info: snap_max_line_cnt =25776
11-07 23:28:39.440: E/QCameraHWI(176): android::status_t android::QCameraHardwareInterface::setPreviewWindow(preview_stream_ops_t*): mPreviewWindow = 0x0xb8aa1780, mStreamDisplay = 0x0xb8a9df90
11-07 23:28:39.440: E/mm-camera(201): config_proc_CAMERA_SET_INFORM_STARTPREVIEW
11-07 23:28:39.450: E/mm-camera(201): config_update_stream_info Storing stream parameters for video inst 1 as : width = 640, height 480, format = 1 inst_handle = 810081 cid = 0
11-07 23:28:39.490: E/mm-camera(201): config_update_stream_info Storing stream parameters for video inst 3 as : width = 640, height 480, format = 1 inst_handle = 830083 cid = 0
11-07 23:28:39.490: E/mm-camera(201): config_update_stream_info Storing stream parameters for video inst 4 as : width = 512, height 384, format = 1 inst_handle = 840084 cid = 0
11-07 23:28:39.500: E/mm-camera(201): config_decide_vfe_outputs: Ports Used 3, Op mode 1
11-07 23:28:39.500: E/mm-camera(201): config_decide_vfe_outputs Current mode 0 Full size streaming : Disabled
11-07 23:28:39.500: E/mm-camera(201): config_decide_vfe_outputs: Primary: 640x480, extra_pad: 0x0, Fmt: 1, Type: 1, Path: 1
11-07 23:28:39.500: E/mm-camera(201): config_decide_vfe_outputs: Secondary: 640x480, extra_pad: 0x0, Fmt: 1, Type: 3, Path: 4
11-07 23:28:39.510: E/mm-camera(201): config_update_inst_handles Updated the inst handles as 810081, 830083, 0, 0
11-07 23:28:39.631: E/mm-camera(201): sensor_load_chromatix: libchromatix_imx119_preview.so: 30
11-07 23:28:39.631: E/mm-camera(201): camif_client_set_params: camif has associated with obj mask 0x1
11-07 23:28:39.631: E/mm-camera(201): config_v2_CAMERA_START_common CAMIF_PARAMS_ADD_OBJ_ID failed -1
11-07 23:28:39.641: E/mm-camera(201): vfe_operation_config: format 3
11-07 23:28:39.641: E/mm-camera(201): vfe_operation_config:vfe_op_mode=5
11-07 23:28:39.641: E/mm-camera(201): Invalid ASD Set Params Type
11-07 23:28:39.641: E/mm-camera(201): vfe_set_bestshot: Bestshot mode not changed