Ngu*_*inh 100 android orientation android-camera-intent
我正在构建一个使用相机拍照的应用程序.这是我的源代码:
File file = new File(Environment.getExternalStorageDirectory(),
imageFileName);
imageFilePath = file.getPath();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, ACTIVITY_NATIVE_CAMERA_AQUIRE);
Run Code Online (Sandbox Code Playgroud)
在onActivityResult()方法上,我BitmapFactory.decodeStream()用来拾取图像.
当我在Nexus上运行我的应用程序时,它运行良好.但是当我在三星Galaxy S或HTC Inspire 4G上运行时,图像的方向是不正确的.

拍摄后的图像预览--------- SD卡上的真实图像
拍摄后的图像预览--------- SD卡上的真实图像
小智 50
这里有很多类似的主题和问题.既然你没有自己编写相机,我认为可以归结为:
一些设备在保存之前旋转图像,而其他设备只是在照片的exif数据中添加方向标记.
我建议查看照片的exif数据并特别注意
ExifInterface exif = new ExifInterface(SourceFileName); //Since API Level 5
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Run Code Online (Sandbox Code Playgroud)
由于照片在您的应用中正确显示,我不确定问题出在哪里,但这绝对应该让您走在正确的道路上!
小智 28
我刚遇到同样的问题,并用它来纠正方向:
public void fixOrientation() {
if (mBitmap.getWidth() > mBitmap.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
mBitmap = Bitmap.createBitmap(mBitmap , 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
}
}
Run Code Online (Sandbox Code Playgroud)
如果位图的宽度大于高度,则返回的图像是横向的,因此我将其旋转90度.
希望它可以帮助其他人解决这个问题.
小智 21
有两件事需要:
相机预览需要与旋转相同.设置它camera.setDisplayOrientation(result);
将拍摄的照片保存为相机预览.通过这样做Camera.Parameters.
int mRotation = getCameraDisplayOrientation();
Camera.Parameters parameters = camera.getParameters();
parameters.setRotation(mRotation); //set rotation to save the picture
camera.setDisplayOrientation(result); //set the rotation for preview camera
camera.setParameters(parameters);
Run Code Online (Sandbox Code Playgroud)希望有所帮助.
小智 10
int rotate = 0;
try {
File imageFile = new File(sourcepath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Run Code Online (Sandbox Code Playgroud)
另一种选择是在结果屏幕中旋转位图,如下所示:
ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.refresh);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
img.setImageDrawable(bmd);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73399 次 |
| 最近记录: |