Android相机已被弃用

时间:2017-03-31 11:31:09

标签: android camera deprecated android-camera2

我有一个Android应用,可以使用已弃用的SurfaceView拍摄一张显示hardware.Camera预览的简单照片。

我是这样做的:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.primeracapa);

    ajustes = getSharedPreferences("ajustes", MODE_PRIVATE);

    mySurfaceView = (SurfaceView) findViewById(R.id.surface);
    mySurfaceHolder = mySurfaceView.getHolder();
    mySurfaceHolder.addCallback(this);

    myInflater = LayoutInflater.from(this);
    View overView = myInflater.inflate(R.layout.segundacapa,null);
    this.addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

    takePicture = (Button) findViewById(R.id.button);
    takePicture.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(capturar==true || !ajustes.getBoolean("longitudAuto", true))
            {
                myCamera.takePicture(myShutterCallback, myPictureCallback, myJpeg);
            }
        }
    });
}

ShutterCallback myShutterCallback = new ShutterCallback() {
    @Override
    public void onShutter() {
    }
};

PictureCallback myPictureCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera myCamera) {
        // Despues de hacer la foto que tiene que hacer
    }
};

PictureCallback myJpeg = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera myCamera) {
        if(data != null){

            File dir_image2 = new File (Environment.getExternalStoragePublicDirectory
                    (Environment.DIRECTORY_DCIM), "foto");
            dir_image2.mkdirs();

            File tmpFile = new File(dir_image2,"TempGhost.jpg");

            try 
            {  
                FileOutputStream fos = new FileOutputStream(tmpFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
            }

            if (!dir_image2.mkdirs()) {
                Log.e("TAG", "Directory not created");
            }
        }
    }
};


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    try{
        if(myPreviewRunning){
            myCamera.stopPreview();
            myPreviewRunning = false;
        }
        Camera.Parameters p = myCamera.getParameters();
        p.setJpegQuality(50);
        p.setPreviewSize(width,height);

        myCamera.setParameters(p);
        myCamera.setPreviewDisplay(holder);
        myCamera.startPreview();
        myPreviewRunning = true;
    }catch(Exception e){}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
    myCamera.stopPreview();
    myPreviewRunning = false;
    myCamera.release();
    myCamera = null;
}

@Override
public void surfaceCreated(SurfaceHolder holder) 
{
    myCamera = Camera.open();
}

我现在需要做的就是不推荐使用Camera就是做同样的事情"简单"使用camera2的事情,它让我发疯了。 camera2是否可能更难以使用。 我只需要在我的SurfaceView上看到相机的预览,并且可以在按下按钮时拍照并将其保存在我创建的特定文件夹中。

1 个答案:

答案 0 :(得分:0)

还在玩相机2,但在我的情况下用于视频预览。 也许你不需要重复请求而是简单的请求。希望这有助于你开始。

CameraManager manager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
      if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        try {
          manager.openCamera(cameraId, new CameraDevice.StateCallback() {


                @Override
                public void onOpened(@NonNull CameraDevice camera) {
                    try {

                        CaptureRequest.Builder builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
                        builder.set(CaptureRequest.CONTROL_MODE,CaptureRequest.CONTROL_MODE_USE_SCENE_MODE);
                        builder.set(CaptureRequest.CONTROL_SCENE_MODE,CaptureRequest.CONTROL_SCENE_MODE_PORTRAIT);

                        builder.addTarget(preview);
                        final CaptureRequest req = builder.build();

                        camera.createCaptureSession(Arrays.asList(preview), new CameraCaptureSession.StateCallback() {
                            @Override
                            public void onConfigured(@NonNull CameraCaptureSession session) {
                                try {

                                    currentSession = session;
                                    session.setRepeatingRequest(req, new CameraCaptureSession.CaptureCallback() {
                                    },new Handler());
                                } catch (CameraAccessException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                                //do something
                            }
                        },new Handler());
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onDisconnected(@NonNull CameraDevice camera) {
                    camera.close();

                }

                @Override
                public void onError(@NonNull CameraDevice camera, int error) {

                }
            }, new Handler());
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }