简体   繁体   English

mCamera.takePicture(null,null,mPicture)不起作用

[英]mCamera.takePicture(null, null, mPicture) does not work

I follow https://developer.android.com/guide/topics/media/camera.html to activate the custom camera for android 4 but my capture function are totally not working. 我遵循https://developer.android.com/guide/topics/media/camera.html激活android 4的自定义相机,但是我的捕获功能完全无法正常工作。 Below is my code: 下面是我的代码:

cameraf.java: cameraf.java:

public class cameraf extends AppCompatActivity{
private Preview mPreview;
private Camera mCamera;
private static final String TAG = "Myact";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cameraf);
    mCamera = getCameraInstance();
    mPreview = new Preview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
   preview.setOnTouchListener(new View.OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){
            Log.v(TAG, "will now take picture");
            mCamera.takePicture(null, null, mPicture);
            return true;
        }
    });
    preview.addView(mPreview);
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v(TAG, "Getting output media file");
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.v(TAG, "Error creating output file");
            return;}
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.v(TAG, e.getMessage());
        } catch (IOException e) {
            Log.v(TAG, e.getMessage());}
    }
};
public static final int MEDIA_TYPE_IMAGE = 1;
private static File getOutputMediaFile(int type){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp");
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;}
    }
    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;
}
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
    }
    return c; // returns null if camera is unavailable
}
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    public Preview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.setDisplayOrientation(90);
            Camera.Parameters params = mCamera.getParameters();
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(params);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;}
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.startPreview();
    }
}
private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}
}

And I have add the permission in manifest which is uses-permission android:name="android.permission.CAMERA",uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 而且我在清单中添加了权限,即使用权限android:name =“ android.permission.CAMERA”,使用权限android:name =“ android.permission.WRITE_EXTERNAL_STORAGE”

But from above code it able let me open the camera but when i tap on the screen it become error and cannot store the image. 但是从上面的代码来看,它可以让我打开相机,但是当我在屏幕上点击时,它会出错并且无法存储图像。 So anyone can share me ideas? 这样任何人都可以分享我的想法?

Error: 错误: 在此处输入图片说明

The failure you see could happen because you do not set picture size. 您看到的失败可能是由于未设置图片大小而导致的。 Some devices require this. 某些设备需要此功能。

Note that you must choose one of supported sizes, and also make sure that the picture size and the preview size are in sync (ie have same aspect ratio). 请注意,您必须选择一种受支持的尺寸,并确保图片尺寸和预览尺寸同步(即具有相同的宽高比)。

You cannot set preview size derived from the surface view size … actually, you don't: your call 您无法设置从表面视图尺寸得出的预览尺寸…实际上,您没有:您的通话

parameters.setPreviewSize(w, h);

is not followed by 后面没有

mCamera.setParameters(parameters);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM