java - 在相机意图android中检测前置或后置摄像头
问题描述
我想通过拍照来获得图像。但是当我使用前置摄像头时,我得到了镜像(旋转),所以我尝试使用这个代码来旋转它
bitmap = rotateImage(bitmap, 270);
但这也会旋转后置摄像头。那么如何检测它是使用前置摄像头还是后置摄像头呢?
检查哪个摄像头是 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;
}
解决方案
您可以使用 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];
我希望这有帮助
推荐阅读
- python - 当所有表达式都在 regex101.com 上工作时,Python 正则表达式不工作,当试图打开 8 " W -> 8"W 和 8 "otherword -> 8" otherword
- python - cv2 帧到视频字节数据,用于流光 st.video
- iis - 查看 IIS 日志中的 SQL 语句/查询参数?
- selenium - 没有 Selenium 的 Whatsapp 消息请求
- google-sheets - 从 Google 表格上的每 8 个单元格中复制一个公式
- hadoop - Hadoop 发布具有不可用名称的指标
- javascript - 有人可以帮我解释为什么这个常量返回 null
- r - 如何将嵌套列表中的数据框更改为列表
- c# - 使用 AJAX 将文本框中的值传递给控制器操作
- apache-kafka - 来自和到文件的 Kafka Avro 控制台消费者/生产者