相机的surfaceview在Android的棒棒糖os不工作

今天我在 Android平面视图中遇到了一个问题,用于相机定制.
我尝试了下面的代码.

The Issue occurred when I captured the image, it stops the camera
preview and doesn’t return to the activity.

以下代码将在程序中实现.我从Stackoverflow中的现有参考中获取了此代码

>支持班

public class AndroidCameraSurfaceview extends Activity implements
    SurfaceHolder.Callback {
TextView testView;

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean preview;

PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
int displayheight, displaywidth;
Camera.PreviewCallback previewCallback;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.camerasurfaceview);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    surfaceHolder = surfaceView.getHolder();

    surfaceHolder.addCallback(this);

    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Bundle b = new Bundle();
            b.putByteArray("Image", data);
            Intent intent = new Intent();
            intent.putExtras(b);
            setResult(RESULT_OK, intent);
            finish();
            // refreshCamera();
        }
    };
}



public void captureImage(View v) throws IOException {
            // take the picture
            camera.takePicture(null, null, jpegCallback);
        }

    public void refreshCamera() {
        if (surfaceHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        try {
            camera.stopPreview();
        } catch (Exception e) {
        }

        try {
            camera.setDisplayOrientation(90);
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (Exception e) {

        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        if (preview) {
            camera.stopPreview();
        }
try{
        Camera.Parameters parameters = camera.getParameters();
        List<Size> sizes = parameters.getSupportedPreviewSizes();
        Size optimalSize = getOptimalPreviewSize(sizes, width, height);
        parameters.setPreviewSize(optimalSize.width, optimalSize.height);
        camera.setParameters(parameters);
        try {
            camera.setDisplayOrientation(90);
            camera.setPreviewDisplay(holder);
            camera.startPreview();
            preview = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
}catch(Exception e){
    System.out.println("Surface Exception---=>"+e);
}
    }

    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        if (camera != null) {
            Camera.Parameters params = camera.getParameters();
            camera.setDisplayOrientation(90);
            camera.setParameters(params);

        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // stop preview and release camera
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 1;
        double targetRatio = (double) w / h;
        if (sizes == null)
            return null;
        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;
        int targetHeight = h;
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    }

2.在活动中实现

public void captureImage()

Intent intentDriver = new Intent(AddNewDevice_Activity.this,
            AndroidCameraSurfaceview.class);
    startActivityForResult(intentDriver, 0);
    //
    // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //
    // Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    //
    // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //
    // // start the image capture Intent
    // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

    // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //
    // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    //
    // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //
    // // start the image capture Intent
    // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0) {

        System.out.println("Result Code: " + resultCode);
        if (resultCode == RESULT_OK && data != null) {

            Bundle bundle = data.getExtras();
            byte[] test = bundle.getByteArray("Image");
            Bitmap bpCamera = BitmapFactory.decodeByteArray(test, 0,
                    test.length);
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            bpCamera = Bitmap
                    .createBitmap(bpCamera, 0, 0, bpCamera.getWidth(),
                            bpCamera.getHeight(), matrix, true);

            imageView_camera.setImageBitmap(bpCamera);
            selectedImageStr = encodeTobase64(bpCamera);
        }

    } else {
        finish();
    }

}
Google已经从API版本21修改了摄像机API,因此,我们必须采用新的camera2软件包,并且必须遵守相机功能的情况.以下是由Google发布的示例代码的链接,该代码使用表面视图实现,它在Android 5.0中完美无瑕.我相信它解决了一点神秘.

https://github.com/googlesamples/android-Camera2Basic

相关文章
相关标签/搜索