简体   繁体   中英

how can i detect the front camera and use to capture an image?

I have my codes here that captured an image automatically using back camera and saved it to the gallery, my problem is, I want the front camera to use to capture an image. How can i detect the front camera? I really need help for this.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    iv_image = (ImageView) findViewById(R.id.imageView);  


    sv = (SurfaceView) findViewById(R.id.surfaceView);  
    sHolder = sv.getHolder();  
    sHolder.addCallback(this);  
    sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  

}  

@Override  
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)  
{  
     parameters = mCamera.getParameters();  

     mCamera.setParameters(parameters);  
     //mCamera.startPreview();  

     Camera.PictureCallback mCall = new Camera.PictureCallback()  
     {  
         @Override  
         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(TakePictureActivity.this,
                         "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();
             }
             catch (FileNotFoundException e) {
                 e.printStackTrace();
             }catch (IOException e) {
                 e.printStackTrace();
             }
             mCamera.startPreview();

             bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
             iv_image.setImageBitmap(bmp);  
         }  
     };  

     mCamera.takePicture(null, null, mCall);
}  

@Override  
public void surfaceCreated(SurfaceHolder holder)  
{   
    mCamera = Camera.open();  
    try {  
       mCamera.setPreviewDisplay(holder);  

    } catch (IOException exception) {  
        mCamera.release();  
        mCamera = null;  
    }  
}  

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

}

If you are using API level 9 (Android 2.3) or above, you can do the following to calculate the index of the (first) front-facing camera:

int getFrontCameraId() {
    CameraInfo ci = new CameraInfo();
    for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, ci);
        if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
    }
    return -1; // No front-facing camera found
}

you can then use the index for the Camera.open method, eg:

int index = getFrontCameraId();
if (index == -1) error();
Camera c = Camera.open(index);

To get the relevant camera. Credits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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