0

以下代码将打开相机意图并从临时 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();
        }
    }
}
4

1 回答 1

2

您可以将文件名附加到意图以将图像保存到文件中。试试这个代码:

    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.

ExifInterface exif = new ExifInterface(f);
String rotation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int rotation = Integer.valueOf(rotation);

然后,尝试旋转位图

于 2013-11-07T13:01:40.997 回答