Detect camera used (front-facing or rear-facing ca

2019-09-05 16:41发布

问题:

I want to detect which camera that my user used to capture an image, to then initialize a value according to the type (Front/Rear camera).

The following is my trying, After I run the app, value is always 0.417 !

public class Home extends Activity {
double value = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.MainActivity);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    input = new EditText(this);
    builder.setView(input);

    builder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int witch) {
            Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = getFile();
            camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(camera_intent, CAM_REQUEST);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAM_REQUEST && resultCode == RESULT_OK) {
            Intent intent = new Intent(this, Enrollment.class);
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                this.value = 0.417;
            }
            else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
                this.value = 0.5;
            }
            startActivity(intent);                
    }
}

BTW, capture image work perfectly, I just need to determine the type of camera (front or rear).

回答1:

You have no way of knowing whether the image came from a front-facing camera, a rear-facing camera, or anything else (an IP camera, a fake picture, etc.). You are delegating the image capture to a third-party app. What that third party app does is up to that app's developers and the user.



回答2:

I wouldn't say this is a guaranteed solution, but it should at least get you something. Each image file will have an ExifInterface which lists various details about the image. You can compare a field (focal length e.g.) on the image with the physical hardware values using the camera libraries.