在我的应用程序中,我必须从相机捕获图像或从库中导入,在活动中的imagevIEw中显示它.一切都很好,我从两者都获得图像,并能够毫无例外地在imagevIEw上设置它.但有时图像不能正确缩放并垂直拉伸或方向改变.请帮帮我.
这是我的代码解码从官方Android文档引用的图像:
public static Bitmap decodeSampledBitmapFromresource(file photofile, int reqWIDth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; try { BitmapFactory.decodeStream(new fileinputStream(photofile), null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWIDth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(new fileinputStream(photofile), null, options); } catch (fileNotFoundException e) { // Todo auto-generated catch block e.printstacktrace(); return null; }}public static int calculateInSampleSize(BitmapFactory.Options options, int reqWIDth, int reqHeight) { // Raw height and wIDth of image final int height = options.outHeight; final int wIDth = options.outWIDth; int inSampleSize = 1; if (height > reqHeight || wIDth > reqWIDth) { // Calculate ratios of height and wIDth to requested height and // wIDth final int heightRatio = Math.round((float) height / (float) reqHeight); final int wIDthRatio = Math.round((float) wIDth / (float) reqWIDth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and wIDth. inSampleSize = heightRatio < wIDthRatio ? heightRatio : wIDthRatio; } return inSampleSize;}
解决方法:
图像具有不同的方向,因此在进行imagevIEw时它会根据方向旋转.您可以从图像属性检查照片的方向.
要以正确的方式设置图像,您可以使用以下代码…
int rot=getCameraPhotoOrIEntation(this,Uri,picturePath); if(rot!=0) bitmap=new RotateOrIEntation().RotateOrIEntationCall(bitmap,rot);
getCameraPhotoOrIEntation方法: –
public static int getCameraPhotoOrIEntation(Context context, Uri imageUri, String imagePath){ int rotate = 0; try { context.getContentResolver().notifyChange(imageUri, null); file imagefile = new file(imagePath); ExifInterface exif = new ExifInterface( imagefile.getabsolutePath()); int orIEntation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_norMAL); switch (orIEntation) { case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; } Log.d(TAG, "Exit orIEntation: " + orIEntation); } catch (Exception e) { e.printstacktrace(); } return rotate; }
添加RotateOrIEntation类以根据方向旋转类.
public class RotateOrIEntation {Bitmap RotateOrIEntationCall(Bitmap src,float degree) { Matrix matrix=new Matrix(); matrix.postRotate(degree); Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWIDth(), src.getHeight(), matrix, true); return bmOut; } }
总结 以上是内存溢出为你收集整理的在imageview中使用时从相机或图库拍摄的照片,其方向发生变化,有时在Android中垂直拉伸全部内容,希望文章能够帮你解决在imageview中使用时从相机或图库拍摄的照片,其方向发生变化,有时在Android中垂直拉伸所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)