[英]Rotate automatically photos taken with Smartphone
I'm having some issues with photos taken with the mobile phone camera. 我用手机相机拍摄的照片出现问题。 When I take a photo and I save it, my mobile phone shows it with the original size, but when I try to open the same photo on my application, it outputs a rotated photo, just like this:
拍照并保存后,手机会以原始尺寸显示它,但是当我尝试在应用程序中打开同一张照片时,它会输出旋转的照片,如下所示:
Original photo: 原始照片:
Rotated photo: 旋转照片:
This issue only occurs when I take photos with my mobile phone. 仅当我用手机拍照时才会出现此问题。
I'm using the following code to select the gallery: 我正在使用以下代码选择图库:
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();
}
To choose the photo: 选择照片:
@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();
}
}
}
I tried to fix it using the following code, but it doesn't work on landscape photos, I would like to get a code to output the original size. 我尝试使用以下代码修复它,但不适用于风景照,我想获取一个代码以输出原始大小。
if (thumbnail.getWidth() > thumbnail.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
thumbnail = Bitmap.createBitmap(thumbnail , 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true);
}
Thanks. 谢谢。
you need to query orientation from mediastore (it's ImageColumns.ORIENTATION) . 您需要从mediastore(它是ImageColumns.ORIENTATION)查询方向。
Try change this line : matrix.postRotate(90);
尝试更改此行:
matrix.postRotate(90);
to matrix.postRotate(270);
到
matrix.postRotate(270);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.