在相机意图android中检测前置或后置摄像头

问题描述 投票:0回答:1

我想拍照留念。但是当我使用前置摄像头时,我正在获得镜像(旋转),因此我尝试使用此代码旋转它

bitmap = rotateImage(bitmap, 270);

但这也会旋转后置摄像头。那么如何使用前置或后置摄像头检测到它?

Check which camera is Open Front or Back Android

我做了一些研究,发现了这个问题。但是不推荐使用cameraInfo,我应该把相机信息放在哪里? selectImage()或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;
}
java android android-camera
1个回答
0
投票

您可以使用Camera2 API查找前置摄像头,您可以尝试使用此代码

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

找到带有id的前置摄像头

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 0表示背面,1表示正面

cameraId = manager.getCameraIdList()[1];

我希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.