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

半城伤御伤魂 提交于 2019-12-12 00:35:25

问题


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.



来源:https://stackoverflow.com/questions/36875136/detect-camera-used-front-facing-or-rear-facing-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!