将相机方向锁定为纵向
如果我像这样启动相机,如何防止 android 相机将方向切换为横向:
How can I prevent the android camera to switch orientation to landscape if I start the camera like this:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imageFolder = new File (Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
imageFile = new File (imageFolder, UUID.randomUUID().toString()+".png");
Uri uriImage = Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
startActivityForResult(intent, 0);
所以我没有特定的相机活动.在 manifest.xml 中,整个应用程序设置为纵向,但相机切换.
So I don't have a particular camera activity. In the manifest.xml the whole appliaction is set to portrait orientation, but the camera switches.
第二个问题,拍照并设置为imageView后,即使我在纵向模式下拍摄它也不会在orientatien中切换(我在设置imageView之前保存了图像),我该如何在正确的位置显示它?
Second Problem, after taking picture and setting it to the imageView it ist switched in orientatien even if I took it in portrait mode (I save the image before I set the imageView), how can I display it in the right posititon?
尝试将相机方向锁定为纵向,
try this for lock camera orientation to portrait,
// *************************************************************************//
// Stop the screen orientation changing during an event
// *************************************************************************//
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
}
private void lockScreenRotation(int orientation)
{
// Stop the screen orientation changing during an event
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
// *************************************************************************//
// store the file url as it will be null after returning from camera app
// *************************************************************************//
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
在 onActivityResult 上设置此项以获得正确位置的图片
Set this on onActivityResult for getting picture in right position
if (resultCode == Activity.RESULT_OK
&& requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
{
filePath = fileUri.getPath();
Utility.log("FILE PATH CAMEREA:->", filePath);
// AppSharedPrefrence.getInstance(getActivity()).setImagePath(path);
// *************************************************************************//
// set default camera rotation
// *************************************************************************//
try
{
File f = new File(filePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
{
angle = 90;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
{
angle = 180;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
{
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
Bitmap bmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);
OutputStream stream = new FileOutputStream(filePath);
bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);
// imageBmp =
// Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path),
// 400, 400, true);
imageBmp = BitmapFactory.decodeFile(filePath);
imgProfilePic.setImageBitmap(imageBmp);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (OutOfMemoryError oom)
{
oom.printStackTrace();
}
}