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



我想通过拍照来获取图像。但是当我使用前置摄像头时,我得到了镜像(旋转(,所以我尝试使用此代码旋转它

bitmap = rotateImage(bitmap, 270);

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

检查哪个相机是打开正面或背面安卓

我做了一些研究,发现了这个问题。 但是相机信息已被弃用,我应该将相机信息放在哪里? 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;
}

您可以使用 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();
}

注意:相机 ID 0 表示背面,1 表示正面

cameraId = manager.getCameraIdList()[1];

我希望这有帮助

最新更新