位图工具BitmapUtil


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class BitmapUtil {


    /**
     * 解码图像
     *
     * @param source
     * @param maxWidth缩略图的实际宽度
     * @param maxHeight缩略图的实际高度
     * @return
     */
    public static Bitmap decodeBitmap(byte[] source, int maxWidth, int maxHeight) {

        //BitmapFactory.Options用于解码Bitmap时的各种参数控制
        BitmapFactory.Options opt = new BitmapFactory.Options();
        //先取宽高     如果将这个值置为true,那么在解码的时候将不会返回bitmap,只会返回这个bitmap的尺寸。这个属性的目的是,如果你只想知道一个bitmap的尺寸,但又不想将其加载到内存时。这是一个非常有用的属性
        opt.inJustDecodeBounds = true;
        //解析宽高(解析出的bitmap为null)
//        BitmapFactory.decodeFile(path, opt);
        BitmapFactory.decodeByteArray(source, 0, source.length, opt);
        configOption(opt, maxWidth, maxHeight);
        //解码图像,要获取像素点
        opt.inJustDecodeBounds = false;
//        Bitmap bitmap = BitmapFactory.decodeFile(path, opt);
        Bitmap bitmap = BitmapFactory.decodeByteArray(source, 0, source.length, opt);
        return bitmap;
    }


    private static void configOption(BitmapFactory.Options opt, int maxWidth, int maxHeight) {

//图片的实际宽高

        int oWidth = opt.outWidth;

        int oHeight = opt.outHeight;

//向上取整,得到缩放比例

        int scW = (int) Math.ceil(oWidth / maxWidth);
        int scH = (int) Math.ceil(oHeight / maxHeight);
        if (scW > 1 || scH > 1) {
            if (scH > scW) {

//这个值是一个int,当它小于1的时候,将会被当做1处理,如果大于1,那么就会按照比例(1 / inSampleSize)缩小bitmap的宽和高、降低分辨率,大于1时这个值将会被处置为2的倍数。例如,width=100,height=100,inSampleSize=2,那么就会将bitmap处理为,width=50,height=50,宽高降为1 / 2,像素数降为1 / 4。
                opt.inSampleSize = scH;  //必须要大于1,必须是整数,最终显示的是1/opt.inSampleSize
            } else {
                opt.inSampleSize = scW;
            }
        }
    }




    /**
     * 从本地加载
     *
     * @param path本地路径
     * @param maxWidth
     * @param maxHeight
     * @return
     */
    public static Bitmap decodeBitmap(String path, int maxWidth, int maxHeight) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        //先取宽高
        opt.inJustDecodeBounds = true;
        //解析宽高(解析出的bitmap为null)
        BitmapFactory.decodeFile(path, opt);
        configOption(opt, maxWidth, maxHeight);
        //解码图像,要获取像素点
        opt.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(path, opt);
        return bitmap;
    }


    /**
     * 将流转为byte数组
     *
     * @param in
     * @return
     */
    public static byte[] stream2ByteArray(InputStream in) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] result = null;
        try {
            byte[] buffer = new byte[1024];
            int num;
            while ((num = in.read(buffer)) != -1) {
                out.write(buffer, 0, num);
            }
            out.flush();
            //转为byte数组
            result = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    /**
     * 保存图像
     *
     * @param bitmap
     * @param path
     * @return
     */
    public static boolean saveBitmap(Bitmap bitmap, String path) {
        boolean result = false;
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);

 //将位图的压缩版本写入到指定的outputStream
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
            out.flush();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


}


在mainfest中加读写权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值