我想通过拍照获取图像。但是当我使用前置摄像头时,我得到的是镜像(旋转),所以我尝试使用此代码旋转它
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;
}
答案 0 :(得分: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];
我希望这对您有帮助