Android图片操作工具类
来源:互联网 发布:linux下中文显示乱码 编辑:程序博客网 时间:2024/06/05 19:58
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); } }}
0 0
- Android图片操作工具类
- 【Android】图片操作工具类(ImageUtil.java)
- android 图片工具类
- Android图片处理工具类
- android 图片工具类代码
- 图片管理Android工具类
- Android图片工具类代码
- android 图片处理工具类
- android 处理图片工具类
- Android图片上传工具类
- Android图片处理工具类
- Android 图片工具类整理
- Android 图片切割工具类
- Android:上传图片工具类
- android圆形图片工具类
- 图片文件操作工具类---ImageUtil
- 【Android】图片操作之 —— Bitmap,byte[],Drawable,InputStream相互转化工具类
- Android文件操作工具类
- eclipse怎样对java代码自动排版,快速格式化,快速使代码对齐?
- Google map api 初学
- 最长公共子序列LCS
- unity3d 5 InputField 非法路径文件名字符 输入过滤
- PAT-JAVA-5-2 然后是几点 (15分)
- Android图片操作工具类
- 编写函数将字符串按逆序存放
- 设计模式学习笔记--享元模式
- Java 动态代理Proxy和Invacationhandler的使用
- hdu 1176 免费馅饼
- jsonp 跨域的使用
- Android 实现一键切换应用主题颜色(二)
- 关于Java变量的可见性问题
- Maven利用Profile构建不同环境的部署包