代码之家  ›  专栏  ›  技术社区  ›  Lester

在摄像头意向android中检测前置或后置摄像头

  •  0
  • Lester  · 技术社区  · 6 年前

    bitmap = rotateImage(bitmap, 270);
    

    Check which camera is Open Front or Back Android

    我做了一些研究,发现了这个问题。但是cameraInfo已经被弃用了,我应该把相机信息放在哪里呢?选择Image()或onActivityResult?

    private void selectImage() {
        try {
            PackageManager pm = context.getPackageManager();
            int hasPerm = pm.checkPermission(Manifest.permission.CAMERA, context.getPackageName());
            if (hasPerm == PackageManager.PERMISSION_GRANTED) {
                final CharSequence[] options = {"Take Photo", "Choose From Gallery","Cancel"};
                android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
                builder.setTitle("Select Option");
                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals("Take Photo")) {
                            dialog.dismiss();
                            dispatchTakePictureIntent();
                        }
                    }
                });
                builder.show();
            } else
                ..
        } catch (Exception e) {
            ...
        }
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        inputStreamImg = null;
    
        if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
            PackageManager pm = context.getPackageManager();
            int hasPerm = pm.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, context.getPackageName());
            if (hasPerm == PackageManager.PERMISSION_GRANTED) {
    
                Uri mUri = null;
                galleryAddPic();
                setPic();
    
                bitmap = rotateImage(bitmap, 270);
                mUri = getImageUri(getContext(), bitmap);
    
                goProgressDetailPage(mUri.toString());
            }else{
                ActivityCompat.requestPermissions(getActivity(), new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
            }
        }
    }
    
    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Log.d("EEERR", ex.getLocalizedMessage());
            }
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(getContext(),
                        "sweat.com.xover.my.sweat.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
    
    private File createImageFile() throws IOException {
        //copy from https://developer.android.com/training/camera/photobasics
    }
    
    private void galleryAddPic() {
        //copy from https://developer.android.com/training/camera/photobasics
    }
    
    private void setPic() {
        //copy from https://developer.android.com/training/camera/photobasics
    }
    
    private Bitmap imageOreintationValidator(Bitmap bitmap, String path) {
    
        ExifInterface ei;
        try {
            ei = new ExifInterface(path);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    bitmap = rotateImage(bitmap, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    bitmap = rotateImage(bitmap, 180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    bitmap = rotateImage(bitmap, 270);
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return bitmap;
    }
    
    private Bitmap rotateImage(Bitmap source, float angle) {
    
        Bitmap bitmap = null;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        try {
            bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                    matrix, true);
        } catch (OutOfMemoryError err) {
            err.printStackTrace();
        }
        return bitmap;
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Rohit Chauhan    6 年前

    您可以使用Camera2 API来查找前置摄像头

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
            try {
                return manager.getCameraIdList();
            } catch (CameraAccessException e) {
                return null;
            }
    

    CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
    
        if (cameraCharacteristics == null)
            throw new NullPointerException("No camera with id " + cameraId);
    
        return cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
    

    然后你必须设置摄像机id

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
       try {
         characteristics = manager.getCameraCharacteristics(mCameraId);
         Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
         if (facing != null && facing ==
        CameraCharacteristics.LENS_FACING_FRONT) {
             //call your method to rotate camera
    }
     }  catch (CameraAccessException e) {
       e.printStackTrace();
     }
    

    cameraId = manager.getCameraIdList()[1];
    

    我希望这有帮助