Android using front facing camera to take a picture

后端 未结 2 1629
执笔经年
执笔经年 2021-02-02 04:01

This is my first time using a camera. I\'ve read a lot of examples and also the documentation, so I tried to make my own class to take pictures with the front facing camera.

相关标签:
2条回答
  • 2021-02-02 04:18

    Take a look at this question for info and code how to do it: Taking pictures with camera on Android programmatically

    The code you used here is not wrong, but this one works.

    If you want to take a pic without the user being shown a preview, take a look at this code: https://github.com/aporter/coursera-android/blob/master/Examples/AudioVideoCamera/src/course/examples/AudioVideo/Camera/AudioVideoCameraActivity.java

    You need to get rid of the SurfaceView that shows the user the preview.

    0 讨论(0)
  • 2021-02-02 04:29

    I solved the problem. After reading some other posts and tutorials seems like that the camera needs a "dummy" surface to preview, even if we don't want the preview to be shown.

    Here's my final code in case anyone needs it. Notice that by "hasCamera" I mean a front facing camera:

       public class CameraController {
    
        private Context context;
    
        private boolean hasCamera;
    
        private Camera camera;
        private int cameraId;
    
        public CameraController(Context c){
            context = c.getApplicationContext();
    
            if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
                cameraId = getFrontCameraId();
    
                if(cameraId != -1){
                    hasCamera = true;
                }else{
                    hasCamera = false;
                }
            }else{
                hasCamera = false;
            }
        }
    
        public boolean hasCamera(){
            return hasCamera;
        }
    
        public void getCameraInstance(){
            camera = null;
    
            if(hasCamera){
                try{
                    camera = Camera.open(cameraId);
                    prepareCamera();
                }
                catch(Exception e){
                    hasCamera = false;
                }
            }
        }
    
        public void takePicture(){
            if(hasCamera){
                camera.takePicture(null,null,mPicture);
            }
        }
    
        public void releaseCamera(){
            if(camera != null){
                camera.stopPreview();
                camera.release();
                camera = null;
            }
        }
    
        private int getFrontCameraId(){
            int camId = -1;
            int numberOfCameras = Camera.getNumberOfCameras();
            CameraInfo ci = new CameraInfo();
    
            for(int i = 0;i < numberOfCameras;i++){
                Camera.getCameraInfo(i,ci);
                if(ci.facing == CameraInfo.CAMERA_FACING_FRONT){
                    camId = i;
                }
            }
    
            return camId;
        }
    
        private void prepareCamera(){
            SurfaceView view = new SurfaceView(context);
    
            try{
                camera.setPreviewDisplay(view.getHolder());
            }catch(IOException e){
                throw new RuntimeException(e);
            }
    
            camera.startPreview();
    
            Camera.Parameters params = camera.getParameters();
            params.setJpegQuality(100);
    
            camera.setParameters(params);  
        }
    
        private PictureCallback mPicture = new PictureCallback(){
            @Override
            public void onPictureTaken(byte[] data, Camera camera){
                File pictureFile = getOutputMediaFile();
    
                if(pictureFile == null){
                    Log.d("TEST", "Error creating media file, check storage permissions");
                    return;
                }
    
                try{
                    Log.d("TEST","File created");
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                }catch(FileNotFoundException e){
                    Log.d("TEST","File not found: "+e.getMessage());
                } catch (IOException e){
                    Log.d("TEST","Error accessing file: "+e.getMessage());
                }
            }
        };
    
        private File getOutputMediaFile(){
            // To be safe, you should check that the SDCard is mounted
            // using Environment.getExternalStorageState() before doing this.
    
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");
    
            // This location works best if you want the created images to be shared
            // between applications and persist after your app has been uninstalled.
    
            // Create the storage directory if it does not exist
            if(!mediaStorageDir.exists()){
                if(!mediaStorageDir.mkdirs()){
                    return null;
                }
            }
    
            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    
            File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg");
    
            return mediaFile;
        }
    }
    
    0 讨论(0)
提交回复
热议问题