设置bitmap的宽高,同时将bitmap转换为file对象

来源:互联网 发布:苹果手机网络被劫持 编辑:程序博客网 时间:2024/05/29 14:32
public class BitmapToSizeChangeFile {    /**     * 将bitmap转换为file存储起来     * @param bitmap     * @return     */    public static File bitmapChangeFile(Bitmap bitmap) {        FileOutputStream fileOutStream = null;        File file = null;        try {            //通过相关方法生成一个Bitmap类型的对象,生产文件选择用当前事件的long型作为文件路径            file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".png");            fileOutStream = new FileOutputStream(file);            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutStream); // 把位图输出到指定的文件中            fileOutStream.flush();        } catch (Exception e) {            e.printStackTrace();        }finally {            try {                fileOutStream.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return file;    }    /**     *  处理图片     * @param bm 所要转换的bitmap     * @param newWidth 新的宽     * @param newHeight 新的高     * @return 指定宽高的bitmap     */    public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){        // 获得图片的宽高        int width = bm.getWidth();        int height = bm.getHeight();        // 计算缩放比例        float scaleWidth = ((float) newWidth) / width;        float scaleHeight = ((float) newHeight) / height;        // 取得想要缩放的matrix参数        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);        // 得到新的图片        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);        return newbm;    }}