Android如何把图片转为深度为32位格式为.bmp的图片

本文介绍如何在Android中将照片转换为32位深度的.bmp格式。通过读取图片文件,转换为Bitmap,然后使用特定方法保存为32位深度的BMP格式,实现了产品的相关需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前几天公司的产品要求做个手机拍的照片转深度为32位,格式为.bmp格式的图片,仔细研究了下,通过调系统相机根据照片存的路径以及FileInputStream获得照片的bitmap,拿到这个bitmap后把数据放到下面的方法里,可获取bmp格式的图片,深度为32位的。

/** 
     * 将Bitmap存为 .bmp格式图片 
     * @param bitmap 
     */  
    public void saveBmp(Bitmap bitmap,long name) {  
        if (bitmap == null)  
            return;  
        // 位图大小  
        int nBmpWidth = bitmap.getWidth();  
        int nBmpHeight = bitmap.getHeight();  
        // 图像数据大小  
       // int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);  
        int bufferSize = nBmpHeight * (nBmpWidth * 4 + nBmpWidth % 5);
        try {  
            // 存储文件名  
           // String filename = savePhoto();  
            String filename =  savePicturePath("HanvonDataBmp");
            File file = new File(filename);  
            if (!file.exists()) {  
                file.createNewFile();  
            }  
            FileOutputStream fileos = new FileOutputStream(filename);  
            // bmp文件头  
            int bfType = 0x4d42;  
            long bfSize = 14 + 40 + bufferSize;  
            int bfReserved1 = 0;  
            int bfReserved2 = 0;  
            long bfOffBits = 14 + 40;  
            // 保存bmp文件头  
            writeWord(fileos, bfType);  
            writeDword(fileos, bfSize);  
            writeWord(fileos, bfReserved1);  
            writeWord(fileos, bfReserved2);  
            writeDword(fileos, bfOffBits);  
            // bmp信息头  
            long biSize = 40L;  
            long biWidth = nBmpWidth;  
            long biHeight = nBmpHeight;  
            int biPlanes = 1;  
           // int biBitCount = 24;  
            int biBitCount = 32;

            long biCompression = 0L;  
            long biSizeImage = 0L;  
            long biXpelsPerMeter = 0L;  
            long biYPelsPerMeter = 0L;  
            long biClrUsed = 0L;  
            long biClrImportant = 0L;  
            // 保存bmp信息头  
            writeDword(fileos, biSize);  
            writeLong(fileos, biWidth);  
            writeLong(fileos, biHeight);  
            writeWord(fileos, biPlanes);  
            writeWord(fileos, biBitCount);  
            writeDword(fileos, biCompression);  
            writeDword(fileos, biSizeImage);  
            writeLong(fileos, biXpelsPerMeter);  
            writeLong(fileos, biYPelsPerMeter);  
            writeDword(fileos, biClrUsed);  
            writeDword(fileos, biClrImportant);  
            // 像素扫描  
            byte bmpData[] = new byte[bufferSize];  
            //int wWidth = (nBmpWidth * 3 + nBmpWidth % 4);  
            int wWidth = (nBmpWidth * 4 + nBmpWidth % 5);
            for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol)  
                for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 4) {  
                    int clr = bitmap.getPixel(wRow, nCol);  
                    bmpData[nRealCol * wWidth + wByteIdex] = (byte) Color.blue(clr);  
                    bmpData[nRealCol * wWidth + wByteIdex + 1] = (byte) Color.green(clr);  
                    bmpData[nRealCol * wWidth + wByteIdex + 2] = (byte) Color.red(clr); 
                    bmpData[nRealCol * wWidth + wByteIdex+3] = (byte) Color.alpha(0xff);
                }  

            fileos.write(bmpData);  
            fileos.flush();  
            fileos.close();  

        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  

    protected void writeWord(FileOutputStream stream, int value) throws IOException {  
        byte[] b = new byte[2];  
        b[0] = (byte) (value & 0xff);  
        b[1] = (byte) (value >> 8 & 0xff);  
        stream.write(b);  
    }  

    protected void writeDword(FileOutputStream stream, long value) throws IOException {  
        byte[] b = new byte[4];  
        b[0] = (byte) (value & 0xff);  
        b[1] = (byte) (value >> 8 & 0xff);  
        b[2] = (byte) (value >> 16 & 0xff);  
        b[3] = (byte) (value >> 24 & 0xff);  
        stream.write(b);  
    }  

    protected void writeLong(FileOutputStream stream, long value) throws IOException {  
        byte[] b = new byte[4];  
        b[0] = (byte) (value & 0xff);  
        b[1] = (byte) (value >> 8 & 0xff);  
        b[2] = (byte) (value >> 16 & 0xff);  
        b[3] = (byte) (value >> 24 & 0xff);  
        stream.write(b);  
    }  

这是一个工具方法,可直接把图片的bitmap放入里面,方法里面的第二个参数name是保存bmp格式图片的名字,savePicturePath(”photoName”)为bmp格式图片保存路径。

public String savePicturePath(String photoName) {
    String mFilePath = Environment.getExternalStorageDirectory().getPath();// 获取SD卡路径
    String fpath = "";
    File file = new File(mFilePath + File.separatorChar + photoName);
    if (!file.exists()) {
        file.mkdirs();
    }
    fpath = mFilePath + File.separatorChar + File.separatorChar + photoName;
    file = new File(fpath);
    if (!file.exists()) {
        file.mkdirs();
    }
    long picName = System.currentTimeMillis();
    String PicturePath = fpath + File.separatorChar + picName + ".png";
    file = null;
    return PicturePath;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值