以下代码将打开相机 Intent 并创建并读取临时 jpg 文件。显示图片文件后,无论我以哪种方式拍摄照片,方向始终是水平的,因此我每次都必须横向握住手机才能拍摄正常照片,我不希望这样。当我以这种方式拍摄照片时,有没有办法将照片更改为正常方向?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ivPhoto = (ImageView) findViewById(R.id.ivPic);
myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.garrenkeith.faceify/files");
System.out.println (myFilesDir);
myFilesDir.mkdirs();
}
public void TakePhoto(View v){
Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(myFilesDir.toString()+"/temp.jpg")));
startActivityForResult(camIntent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==0){
try {
Bitmap cameraBitmap;
cameraBitmap = BitmapFactory.decodeFile(myFilesDir + "/temp.jpg");
Bitmap.createBitmap(cameraBitmap);
ivPhoto.setImageBitmap(cameraBitmap);
}
catch(Exception e){
e.printStackTrace();
}
}
}
最佳答案
您可以将文件名附加到 Intent 上,以将图像保存到文件中。试试这个代码:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
takePictureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, 0);
activity.startActivityForResult(takePictureIntent, requestCode);
在这种情况下,如果旋转不起作用,您可以在onActivityResult
中从文件中获取EXIF信息。
ExifInterface exif = new ExifInterface(f);
String rotation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int rotation = Integer.valueOf(rotation);
然后,尝试旋转位图
关于java - 相机对话框不保存 exif 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19832986/