package com.example.smart.oss.image; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.widget.ImageView; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class UiDisplayer { private ImageView imageView; public void setImageView(ImageView imageView) { this.imageView = imageView; } //根据ImageView大小自动缩放图片 public Bitmap autoResizeFromStream(InputStream stream) throws IOException { byte[] data; { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = stream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.close(); data = outStream.toByteArray(); stream.close(); } // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, imageView.getWidth(), imageView.getHeight()); Log.d("ImageHeight", String.valueOf(options.outHeight)); Log.d("ImageWidth", String.valueOf(options.outWidth)); Log.d("Height", String.valueOf(imageView.getWidth())); Log.d("Width", String.valueOf(imageView.getWidth())); options.inSampleSize = 4; Log.d("SampleSize", String.valueOf(options.inSampleSize)); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(data, 0, data.length, options); } //计算图片缩放比例 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) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }