Android Bitmap 的一些操作

本文详细介绍了如何使用Android平台的Bitmap类进行图像分辨率调整、旋转及格式转换。包括计算缩放比例并应用到Matrix参数,实现图像的等比缩放;通过Matrix预旋转功能,改变Bitmap的角度;以及将Bitmap保存为PNG格式的文件。

更改 Bitmap 分辨率

private Bitmap SetBmpResolution(Bitmap nBmp, int nWidth, int nHeight) {
        // 原图大小
        int width = nBmp.getWidth();
        int height = nBmp.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) nWidth) / width;
        float scaleHeight = ((float) nHeight) / height;
        // 取得想要缩放的matrix参数  android.graphics.Matrix;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        return Bitmap.createBitmap(nBmp, 0, 0, width, height, matrix, true);
    }

Bitmap 旋转角度

Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata, 0,imgdata.length);
Matrix matrix = new Matrix();
matrix.preRotate(270);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bmbitmapgetHeight(), matrix, true);

Bitmap 保存为PNG格式的文件

 private void BmpToSavePNG(Bitmap nBmp, String nFilePath) {
        try {
            File lFile = new File(nFilePath);
            if (lFile.exists()) {   // 如果文件存在
                if (!lFile.delete()) {
                    Log.d("Test","Delete file failed!");
                }
            }
            //文件输出流
            FileOutputStream out = new FileOutputStream(nFilePath);
            //压缩图片,png:Bitmap.CompressFormat.PNG,jpg:Bitmap.CompressFormat.JPEG,质量100%,表示不压缩
            nBmp.compress(Bitmap.CompressFormat.PNG, 100, out);
            //写入
            out.flush();
            //关闭流
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值