android图片处理

来源:互联网 发布:滁州安广网络客服电话 编辑:程序博客网 时间:2024/05/14 08:29
<span style="font-size:18px;"><span style="white-space:pre"></span>/** * 对位图进行等比例缩放,需要传入要缩放位图的流入量,与原始宽度及高度 * @param inputsream,输入流 * @param width ,需要设置的宽度 * @param height,需要设置的高度 * @return 返回缩放后的图片 * @throws IOException */public static Bitmap Scalebitmap(InputStream inputsream,int width,int height) throws IOException{byte[] buffer = new byte[512];ByteArrayOutputStream bos= new ByteArrayOutputStream();int length;while((length=inputsream.read(buffer))!=-1){bos.write(buffer, 0, length);}byte[] byteArray = bos.toByteArray();Options opts=new Options();opts.inJustDecodeBounds=true;BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, opts);width=opts.outWidth/width;height=opts.outHeight/height;int scale = width>height?width:height;opts.inJustDecodeBounds=false;opts.inSampleSize=scale;Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, opts);return bm;}</span>
<span style="font-size:18px;"><span style="white-space:pre"></span></span>
图片的程序缓存及本地缓存操作:
<pre style="margin-top: 15px; padding: 6px 10px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; color: rgb(51, 51, 51); margin-bottom: 0px !important; background-color: rgb(248, 248, 248);"><code style="margin: 0px; padding: 0px; border: none; font-size: 12px; font-family: Consolas, 'Liberation Mono', Courier, monospace; border-radius: 3px; background: transparent;">=====================================================================// 程序声明缓存数据private Map<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();//将图片保存到软缓存中cache.put(path, new SoftReference<Bitmap>(bm));// 从缓存中寻找看是否有该图SoftReference<Bitmap> softReference = cache.get(path);if (softReference != null) {    Bitmap bm = softReference.get();}======================================================================//将bitmap图片保存到本地缓存目录中String filename = path.substring(path.lastIndexOf("/") + 1);File file = new File(context.getCacheDir(), filename);targetFile=file.getAbsolutePath();// 父目录不存在 则创建父目录    if (!targetFile.getParentFile().exists()) {        targetFile.getParentFile().mkdirs();    }    FileOutputStream fos = new FileOutputStream(targetFile);    bitmap.compress(CompressFormat.JPEG, 100, fos);====================================================================//本地文件缓存中读取图片String filename = path.substring(path.lastIndexOf("/") + 1);File file = new File(MusicApplication.getcontext().getCacheDir(),            filename);File file = new File();//判断本地缓存中是否有该图片if (!file.exists()){        return null;}else{    return BitmapFactory.decodeFile(file.getAbsolutePath());}===================================================================</code>

<span style="font-size:18px;"></span>

1 0
原创粉丝点击