2
我在使用手機相機拍攝的照片時遇到了一些問題。當我拍照並保存時,我的手機會以原始尺寸顯示它,但是當我嘗試在我的應用程序上打開相同的照片時,它會輸出旋轉後的照片,如下所示:自動旋轉智能手機拍攝的照片
原始照片:
旋轉的照片:
,當我帶着我的手機照片只出現此問題。
我用下面的代碼來選擇空間:
private void selectImage() {
final CharSequence[] options = {"My gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(PhotosActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("My gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
要選擇照片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
if (thumbnail.getWidth() > thumbnail.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
thumbnail = Bitmap.createBitmap(thumbnail , 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true);
}
int nh = (int) (thumbnail.getHeight() * (612.0/thumbnail.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(thumbnail, 612, nh, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
scaled.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
newtask = new saveImage();
newtask.execute(image_str,sessionid);
Toast.makeText(getBaseContext(), "Nice!",
Toast.LENGTH_LONG).show();
}
}
}
我嘗試使用下面的代碼來修復它,但它不在風景照片上工作,我想獲得一個代碼來輸出原始大小。
if (thumbnail.getWidth() > thumbnail.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
thumbnail = Bitmap.createBitmap(thumbnail , 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true);
}
謝謝。
我搜索如何讓我找到了解決的方向,我在這裏使用此功能:http://stackoverflow.com/questions/21085105/get-orientation-of-image -from-mediastore-images-media-data和改變的matrix.postRotate(90);到matrix.postRotate(getExifOrientation(picturePath));現在工作正常。 –