Android:将Bitmap对象保存为bmp(1bpp)文件格式

时间:2014-08-22 13:11:25

标签: android image bitmap depth bpp

根据@ ben75:Android : save a Bitmap to bmp file format

提出的问题

我现在的问题是:我怎样才能拥有每像素1位深度的BMP图像(黑白)?

2 个答案:

答案 0 :(得分:2)

回答我自己的问题......

在网上进行了一些艰难的搜索后,我意识到我必须创建两件事:一个黑白位图 - 并且使用为低于128和255的颜色制作所有0的方法做到了这一点其余的,像这样(这是C#代码,因为我使用Xamarin来编写我的应用程序):

private void ConvertArgbToOneColor (Bitmap bmpOriginal, int width, int height){
        int pixel;
        int k = 0;
        int B=0,G=0,R=0;
        try{
            for(int x = 0; x < height; x++) {
                for(int y = 0; y < width; y++, k++) {
                    pixel = bmpOriginal.GetPixel(y, x);

                    R = Color.GetRedComponent(pixel);
                    G = Color.GetGreenComponent(pixel);
                    B = Color.GetBlueComponent(pixel);

                    R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B);
                    if (R < 128) {
                        m_imageArray[k] = 0;
                    } else {
                        m_imageArray[k] = 1;
                    }
                }
                if(m_dataWidth>width){
                    for(int p=width;p<m_dataWidth;p++,k++){
                        m_imageArray[k]=1;
                    }
                }
            }
        }catch (Exception e) {
            System.Console.WriteLine ("Converting to grayscale ex: " + e.Message);
        }
    }

然后获取单色图像的byteArray:

int length = 0;
for (int i = 0; i < m_imageArray.Length; i = i + 8) {
    byte first = m_imageArray[i];
    for (int j = 0; j < 7; j++) {
        byte second = (byte) ((first << 1) | m_imageArray[i + j]);
        first = second;
    }
    m_rawImage[length] = first;
    length++;
}

最后手工制作位图&#34;#34;使用以下变量并将它们放入FileOutputStream中以保存文件:

    private static int FILE_HEADER_SIZE = 14;
    private static int INFO_HEADER_SIZE = 40;

    // Bitmap file header
    private byte[] bfType = { (byte) 'B', (byte) 'M' };
    private int bfSize = 0;
    private int bfReserved1 = 0;
    private int bfReserved2 = 0;
    private int bfOffBits = FILE_HEADER_SIZE + INFO_HEADER_SIZE + 8;

    // Bitmap info header
    private int biSize = INFO_HEADER_SIZE;
    private int biWidth = 0;
    private int biHeight = 0;
    private int biPlanes = 1;
    private int biBitCount = 1;
    private int biCompression = 0;
    private int biSizeImage = 0;
    private int biXPelsPerMeter = 0x0;
    private int biYPelsPerMeter = 0x0;
    private int biClrUsed = 2;
    private int biClrImportant = 2;

    // Bitmap raw data
    private byte[] bitmap;

    // Scanlinsize;
    int scanLineSize = 0;

    // Color Pallette to be used for pixels.
    private byte[] colorPalette = { 0, 0, 0, (byte) 255, (byte) 255,
        (byte) 255, (byte) 255, (byte) 255 };

答案 1 :(得分:0)

Bitmap bmpMonochrome = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bmpMonochrome); 
ColorMatrix ma = new ColorMatrix(); 
ma.setSaturation(0); 
Paint paint = new Paint(); 
paint.setColorFilter(new ColorMatrixColorFilter(ma)); 
canvas.drawBitmap(bmpSrc, 0, 0, paint);
像这样! Source