BitmapUtil

来源:互联网 发布:天下霸唱抄袭证据知乎 编辑:程序博客网 时间:2024/05/17 08:22

/**
* Bitmap工具类
*/
public class BitmapUtil {
private static BitmapUtil bitmapUtil = new BitmapUtil();

public static BitmapUtil getInstance() {    if (bitmapUtil == null) {        synchronized (BitmapUtil.class) {            if (bitmapUtil == null) {                bitmapUtil = new BitmapUtil();            }        }    }    return bitmapUtil;}public boolean savePic(Bitmap bm, String str) {    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {        File dir = new File(str);        if (!dir.getParentFile().exists()) {            dir.getParentFile().mkdirs();        }        File file = new File(str);        FileOutputStream out = null;        try {            out = new FileOutputStream(file);            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);            out.flush();            return true;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            StreamUtils.closeStream(out);        }        return false;    }    return false;}public Bitmap GetLocalOrNetBitmap(String url) {    Bitmap bitmap = null;    InputStream in = null;    BufferedOutputStream out = null;    try {        in = new BufferedInputStream(new URL(url).openStream(), 2 * 1024);        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();        out = new BufferedOutputStream(dataStream, 2 * 1024);        copy(in, out);        out.flush();        byte[] data = dataStream.toByteArray();        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);        data = null;        return bitmap;    } catch (IOException e) {        e.printStackTrace();        return null;    }}private void copy(InputStream in, OutputStream out) throws IOException {    byte[] b = new byte[2 * 1024];    int read;    while ((read = in.read(b)) != -1) {        out.write(b, 0, read);    }}public Bitmap createBitmap(String path) {    try {        if (TextUtils.isEmpty(path) || !FileUtil.getInstance().isFileExist(path)) {            return null;        }        BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeFile(path, options);// 此时返回bm为空        int scale = 1;        while ((options.outWidth / scale > 480)) {            scale += 1;        }        options.inJustDecodeBounds = false;        options.inSampleSize = scale;        return BitmapFactory.decodeFile(path, options);    } catch (Exception e) {        return null;    }}//将Bitmap转换成字符串public String bitmapToString(Bitmap bitmap) {    String string = null;    ByteArrayOutputStream bStream = new ByteArrayOutputStream();    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);    byte[] bytes = bStream.toByteArray();    string = Base64.encodeToString(bytes, Base64.DEFAULT);    return string.trim();}public Bitmap stringtoBitmap(String string) {    //将字符串转换成Bitmap类型    Bitmap bitmap = null;    try {        byte[] bitmapArray;        bitmapArray = Base64.decode(string, Base64.DEFAULT);        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);    } catch (Exception e) {        e.printStackTrace();    }    return bitmap;}public InputStream Bitmap2InputStream(Bitmap bm) {    ByteArrayOutputStream baos = new ByteArrayOutputStream();    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);    InputStream is = new ByteArrayInputStream(baos.toByteArray());    return is;}

}