بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ

چرا عکسهای عمودی گرفته شده با برنامه من افقی نمایش داده می شوند؟

مدیریت گفتگو
ahmad_r 11 سال پیش
آخرین تغییر: 9 سال پیش

من از کد زیر برای گرفتن عکس استفاده می کنم

 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("MyAddress.jpg")));
startActivityForResult(intent, G.REQUEST_CAMERA);

عکس گرفته می شود ولی مشکل اینجاست که اگر عکس را عمودی بگیرم در برنامه من افقی نمایش ولی در گالری پیش فرض گوشی صحیح نمایش می دهد

البته تابع زیر درستش میکنه ولی میخوام اساسی ذخیره بشه که نیازی به استفاده از تابع در هر بار نمایش عکس نباشه.

آخه عکسهایی که مستقیما با خود دوربین گرفته میشه نه برنامه من رو همیشه درست نمایش میده و کاملا مشخصه که نحوه عکس گرفتن من باید درست بشه.

    private static void setImageCorrectlyOriented(String address, ImageView img)
{
Bitmap myBitmap = BitmapFactory.decodeFile(address);

try {

// if (myBitmap.getWidth() > myBitmap.getHeight())
// matrix.postRotate(90);

File imgFile = new File(address);
Matrix matrix = new Matrix();
ExifInterface exif = new ExifInterface(imgFile.getAbsolutePath());

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
}
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true);

}
catch (Exception e) {
e.printStackTrace();
}
img.setImageBitmap(myBitmap);
}

 

نظرات

hoseinnrz 11 سال پیش
@ahmad_r : جوابی برای حل این مشکل پیدا نکردی هنوز ؟ اگه پیدا کردی بی زحمت بگو ما هم مشکل داریم سر این قضیه :)

پاسخ‌ها 2

مجتبی یگانه 11 سال پیش

دقیقا همین کد تو کلیپ بورد ام بود و اومدم براتون درج کنم که دیدم ویرایش کرد و خودتون درج کردید !

حالت پیشفرض دوربین ها حالت LandScape هست . منم چند روز پیش به همین مشکل خوردم و این لینک مشکل رو حل کرد

نظرات

hoseinnrz 11 سال پیش
دستت درد نکنه ، یه خرده باهاش سر و کله بزنیم ببینیم میشه درست پیاده سازیش کرد :)
ahmad_r 11 سال پیش
نه متاسفانه راه حل بهتری هنوز به دست نیاوردم.
moin007 9 سال پیش

File f=new File("addres ax");
image.setImageBitmap(decodeFile(f));


 این کد عکس رو همونجری ک ذخیره شده بر می گردونه 

private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE=70;

// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
این کد هم می چرخونه اگر نتیجه همونی ک میخواید نبود
 
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE=150;

// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();

o2.inSampleSize = scale;
Matrix matrix = new Matrix();
float angle = 90;
matrix.postRotate(angle);

Bitmap bMap= BitmapFactory.decodeStream(new FileInputStream(f), null , o2);
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWidth(),bMap.getHeight(), matrix, true);

return bMapRotate;
} catch (FileNotFoundException e) {}
return null;
}
 مشکل حافظه هم اگر داشته باشید تو عکس های سنگین بر طرف می کنه


نظرات


پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .