bitmap工具类

private boolean saveBitmapfile(Bitmap bmp) {
    Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
    int quality = 100;
    OutputStream stream = null;
    try {
      stream=new FileOutputStream(Environment.getExternalStorageDirectory()+".jpg");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return bmp.compress(format, quality, stream);
}

private File getBitmapfile(Bitmap bmp) {
    Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
    int quality = 100;
    OutputStream stream = null;
    try {
      stream=new FileOutputStream(Environment.getExternalStorageDirectory()+".jpg");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bmp.compress(format, quality, stream);
    return new File(Environment.getExternalStorageDirectory() + ".jpg");
}

public Bitmap readBitMap(Context context, int resId) {

    BitmapFactory.Options opt = new BitmapFactory.Options();

    opt.inPreferredConfig = Bitmap.Config.RGB_565;

    opt.inPurgeable = true;

    opt.inInputShareable = true;

    //  获取资源图片

    InputStream is = context.getResources().openRawResource(resId);

    return BitmapFactory.decodeStream(is, null, opt);

}

public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
                               double newHeight) {
    // 获取这个图片的宽和高
    float width = bgimage.getWidth();
    float height = bgimage.getHeight();
    // 创建操作图片用的matrix对象
    Matrix matrix = new Matrix();
    // 计算宽高缩放率
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // 缩放图片动作
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
            (int) height, matrix, true);
    return bitmap;
}


你可能感兴趣的:(bitmap工具类)