在Android中无需预览即可捕获图像

时间:2013-12-25 19:10:31

标签: android image capture

我有这段代码,但它给了我这个:

Unknown URI: content://media/external/images/media

注意:我有权使用相机/写入。

这是我的代码:

public class MainActivity extends Activity {
    // ...

    Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {

            Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
                                                              new ContentValues());
            OutputStream imageFileOS;
            try {

                imageFileOS = getContentResolver().openOutputStream(uriTarget);
                imageFileOS.write(data);
                imageFileOS.flush();
                imageFileOS.close();

                Toast.makeText(getApplicationContext(), 
                               "Image saved: " + uriTarget.toString(), 
                               Toast.LENGTH_LONG).show();

            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            }

            finish();
        }
    };
    // ...
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

// Here is the example for surface view.

public void takePictureNoPreview(Context context) { // open back facing camera by default 

    Camera myCamera=Camera.open();

    if(myCamera!=null) { 
        try { // set camera parameters if you want to

        // here, the unused surface view and holder

        SurfaceView dummy=new SurfaceView(context);
        myCamera.setPreviewDisplay(dummy.getHolder());
        myCamera.startPreview();
        myCamera.takePicture(null, null, getJpegCallback());

        } finally { 
            myCamera.close(); 
        }
    } else { 
        // booo, failed! 
    }
}

private PictureCallback getJpegCallback() { 
    PictureCallback jpeg = new PictureCallback() { 
        @Override 
        public void onPictureTaken(byte[] data, Camera camera) { 
            FileOutputStream fos; 
            try { 
                fos = new FileOutputStream("test.jpeg"); 
                fos.write(data); fos.close(); 
            } catch (IOException e) { 
                //do something about it 
            } 
        } 
    }; 
    return jpeg;
}

// Selecting front facing camera.

private Camera openFrontFacingCameraGingerbread() { 
    int cameraCount = 0; Camera cam = null; 
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
    cameraCount = Camera.getNumberOfCameras(); 
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) { 
        Camera.getCameraInfo( camIdx, cameraInfo ); 
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) { 
            try { 
                cam = Camera.open( camIdx ); 
            } catch (RuntimeException e) { 
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage()); 
            } 
        } 
    }
    return cam; 
}


// This should work...