Android图片操作工具类
2016-05-10 11:22
537 查看
package com.aliyun.oss.ossdemo; import android.app.Activity; import android.app.AlertDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** * Created by yjs on 2015/12/7 0007. * 完成显示图片操作 */ public class ImageDisplayer { private ImageView imageView; private int height; private int width; public ImageDisplayer(ImageView imageView) { this.imageView = imageView; } public ImageDisplayer(int height, int width) { this.height = height; this.width = width; } public static byte[] getBytesFromStream(InputStream stream) throws IOException { { 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(); return outStream.toByteArray(); } } //计算图片缩放比例 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; } //根据ImageView的大小自动缩放图片 public Bitmap autoResizeFromLocalFile(String picturePath) throws IOException { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(picturePath, options); // Calculate inSampleSize int h = height; int w = width; if (imageView != null) { h = imageView.getHeight(); w = imageView.getWidth(); } options.inSampleSize = calculateInSampleSize(options, w, h); Log.d("ImageHeight", String.valueOf(options.outHeight)); Log.d("ImageWidth", String.valueOf(options.outWidth)); Log.d("Height", String.valueOf(h)); Log.d("Width",String.valueOf(w)); //options.inSampleSize = 10; Log.d("SampleSize", String.valueOf(options.inSampleSize)); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(picturePath, options); } public Bitmap autoResizeFromBytes(byte[] data) { // 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); int h = height; int w = width; if (imageView != null) { h = imageView.getHeight(); w = imageView.getWidth(); } options.inSampleSize = calculateInSampleSize(options, w, h); Log.d("ImageHeight", String.valueOf(options.outHeight)); Log.d("ImageWidth", String.valueOf(options.outWidth)); Log.d("Height", String.valueOf(h)); Log.d("Width",String.valueOf(w)); //options.inSampleSize = 10; Log.d("SampleSize", String.valueOf(options.inSampleSize)); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(data, 0, data.length, options); } //根据ImageView大小自动缩放图片 public Bitmap autoResizeFromStream(InputStream stream) throws IOException { byte[] data = getBytesFromStream(stream); return autoResizeFromBytes(data); } public Bitmap autoResizeFromBitmap(Bitmap bm) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.outHeight = bm.getHeight(); options.outWidth = bm.getWidth(); int h = height; int w = width; if (imageView != null) { h = imageView.getHeight(); w = imageView.getWidth(); } int inSampleSize = calculateInSampleSize(options, w, h); Log.d("ImageHeight", String.valueOf(options.outHeight)); Log.d("ImageWidth", String.valueOf(options.outWidth)); Log.d("Height", String.valueOf(h)); Log.d("Width",String.valueOf(w)); if (inSampleSize == 1) { return bm; } else { return Bitmap.createScaledBitmap(bm, bm.getWidth() / inSampleSize, bm.getHeight() / inSampleSize, true); } } }
相关文章推荐
- 修改 AndroidManifest minSdkVersion 的方法
- Android 快速开发数据库greenDao
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)(源码 + Demo)
- Android调用webservice(线程实现)
- [置顶] Android MVP和SharedPreferences存储JSONArray
- 简单的Android中在Button控件上显示倒计时。
- 导入工程时报错The import android cannot be resolved
- 导入工程时报错The import android cannot be resolved
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Android ScrollView嵌套ListView或者GridView显示不全
- Android杂记(一)
- android逆向工具
- android studio
- Android疑难点解答
- Android事件分发机制(一) Touch 事件的分发和消费机制
- 关于Android VerSionName版本控制问题
- 下载判断Android和iOS
- Android 二代身份证精确校验
- Android录制声音,并播放,功能优化
- Android性能优化 一 优化小结