我正在使用下面的代碼保存在我的應用程序的圖片:垂直圖像旋轉
public void addPhoto(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID,
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_IMAGE_CAPTURE&&resultCode==RESULT_OK){
Intent i = new Intent(Assignments.this, AddAssignment.class);
i.putExtra("fileName", mCurrentPhotoPath);
startActivity(i);
}
}
我再縮放圖像使用下面的代碼來創建一個更小的尺寸預覽:
public Bitmap getImage(){
int targetW;
int targetH;
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileName, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
if(photoW>photoH){
targetW=400;
targetH=300;
}else{
targetW=300;
targetH=400;
}
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(fileName, bmOptions);
photo=bitmap;
return(bitmap);
}
我的問題是,如果拍攝的圖像是垂直的,圖像會翻轉水平。我不知道究竟發生了什麼,我很感激任何幫助。謝謝。
或許事做 '的Exif' 如果你的設備的相機操作這樣。而你並沒有考慮它。 [https://www.howtogeek.com/254830/why-your-photos-dont-always-appear-correctly-rotated/] – Doomsknight