Android Bitmap保存为.bmp格式,图像转化为黑白图片

来源:互联网 发布:复杂网络 什么专业 编辑:程序博客网 时间:2024/04/30 12:06
[java] view plain copy
  1. /**  
  2.      * 把一个View的对象转换成bitmap  
  3.      */  
  4.     static Bitmap getViewBitmap(View v) {  
  5.         v.clearFocus();  
  6.         v.setPressed(false);  
  7.   
  8.         //能画缓存就返回false   
  9.         boolean willNotCache = v.willNotCacheDrawing();  
  10.         v.setWillNotCacheDrawing(false);  
  11.         int color = v.getDrawingCacheBackgroundColor();  
  12.         v.setDrawingCacheBackgroundColor(0);  
  13.         if (color != 0) {  
  14.             v.destroyDrawingCache();  
  15.         }  
  16.         v.buildDrawingCache();  
  17.         Bitmap cacheBitmap = v.getDrawingCache();  
  18.         if (cacheBitmap == null) {  
  19.             Log.e("BtPrinter""failed getViewBitmap(" + v + ")"new RuntimeException());  
  20.             return null;  
  21.         }  
  22.         Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  
  23.         // Restore the view   
  24.         v.destroyDrawingCache();  
  25.         v.setWillNotCacheDrawing(willNotCache);  
  26.         v.setDrawingCacheBackgroundColor(color);  
  27.         return bitmap;  
  28.     }  
  29.   
  30.     /** 
  31.     * 将彩色图转换为黑白图 
  32.     *  
  33.     * @param 位图 
  34.     * @return 返回转换好的位图 
  35.     */  
  36.     public static Bitmap convertToBlackWhite(Bitmap bmp) {  
  37.         int width = bmp.getWidth(); // 获取位图的宽  
  38.         int height = bmp.getHeight(); // 获取位图的高  
  39.         int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组  
  40.   
  41.         bmp.getPixels(pixels, 0, width, 00, width, height);  
  42.         int alpha = 0xFF << 24;  
  43.         for (int i = 0; i < height; i++) {  
  44.             for (int j = 0; j < width; j++) {  
  45.                 int grey = pixels[width * i + j];  
  46.   
  47.                 int red = ((grey & 0x00FF0000) >> 16);  
  48.                 int green = ((grey & 0x0000FF00) >> 8);  
  49.                 int blue = (grey & 0x000000FF);  
  50.   
  51.                 grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);  
  52.                 grey = alpha | (grey << 16) | (grey << 8) | grey;  
  53.                 pixels[width * i + j] = grey;  
  54.             }  
  55.         }  
  56.         Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);  
  57.   
  58.         newBmp.setPixels(pixels, 0, width, 00, width, height);  
  59.   
  60.         Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380460);  
  61.         return resizeBmp;  
  62.     }  
  63.   
  64.     /** 
  65.      * 将Bitmap存为 .bmp格式图片 
  66.      * @param bitmap 
  67.      */  
  68.     private void saveBmp(Bitmap bitmap) {  
  69.         if (bitmap == null)  
  70.             return;  
  71.         // 位图大小  
  72.         int nBmpWidth = bitmap.getWidth();  
  73.         int nBmpHeight = bitmap.getHeight();  
  74.         // 图像数据大小  
  75.         int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);  
  76.         try {  
  77.             // 存储文件名  
  78.             String filename = "/sdcard/test.bmp";  
  79.             File file = new File(filename);  
  80.             if (!file.exists()) {  
  81.                 file.createNewFile();  
  82.             }  
  83.             FileOutputStream fileos = new FileOutputStream(filename);  
  84.             // bmp文件头  
  85.             int bfType = 0x4d42;  
  86.             long bfSize = 14 + 40 + bufferSize;  
  87.             int bfReserved1 = 0;  
  88.             int bfReserved2 = 0;  
  89.             long bfOffBits = 14 + 40;  
  90.             // 保存bmp文件头  
  91.             writeWord(fileos, bfType);  
  92.             writeDword(fileos, bfSize);  
  93.             writeWord(fileos, bfReserved1);  
  94.             writeWord(fileos, bfReserved2);  
  95.             writeDword(fileos, bfOffBits);  
  96.             // bmp信息头  
  97.             long biSize = 40L;  
  98.             long biWidth = nBmpWidth;  
  99.             long biHeight = nBmpHeight;  
  100.             int biPlanes = 1;  
  101.             int biBitCount = 24;  
  102.             long biCompression = 0L;  
  103.             long biSizeImage = 0L;  
  104.             long biXpelsPerMeter = 0L;  
  105.             long biYPelsPerMeter = 0L;  
  106.             long biClrUsed = 0L;  
  107.             long biClrImportant = 0L;  
  108.             // 保存bmp信息头  
  109.             writeDword(fileos, biSize);  
  110.             writeLong(fileos, biWidth);  
  111.             writeLong(fileos, biHeight);  
  112.             writeWord(fileos, biPlanes);  
  113.             writeWord(fileos, biBitCount);  
  114.             writeDword(fileos, biCompression);  
  115.             writeDword(fileos, biSizeImage);  
  116.             writeLong(fileos, biXpelsPerMeter);  
  117.             writeLong(fileos, biYPelsPerMeter);  
  118.             writeDword(fileos, biClrUsed);  
  119.             writeDword(fileos, biClrImportant);  
  120.             // 像素扫描  
  121.             byte bmpData[] = new byte[bufferSize];  
  122.             int wWidth = (nBmpWidth * 3 + nBmpWidth % 4);  
  123.             for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol)  
  124.                 for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 3) {  
  125.                     int clr = bitmap.getPixel(wRow, nCol);  
  126.                     bmpData[nRealCol * wWidth + wByteIdex] = (byte) Color.blue(clr);  
  127.                     bmpData[nRealCol * wWidth + wByteIdex + 1] = (byte) Color.green(clr);  
  128.                     bmpData[nRealCol * wWidth + wByteIdex + 2] = (byte) Color.red(clr);  
  129.                 }  
  130.   
  131.             fileos.write(bmpData);  
  132.             fileos.flush();  
  133.             fileos.close();  
  134.   
  135.         } catch (FileNotFoundException e) {  
  136.             e.printStackTrace();  
  137.         } catch (IOException e) {  
  138.             e.printStackTrace();  
  139.         }  
  140.     }  
  141.   
  142.     protected void writeWord(FileOutputStream stream, int value) throws IOException {  
  143.         byte[] b = new byte[2];  
  144.         b[0] = (byte) (value & 0xff);  
  145.         b[1] = (byte) (value >> 8 & 0xff);  
  146.         stream.write(b);  
  147.     }  
  148.   
  149.     protected void writeDword(FileOutputStream stream, long value) throws IOException {  
  150.         byte[] b = new byte[4];  
  151.         b[0] = (byte) (value & 0xff);  
  152.         b[1] = (byte) (value >> 8 & 0xff);  
  153.         b[2] = (byte) (value >> 16 & 0xff);  
  154.         b[3] = (byte) (value >> 24 & 0xff);  
  155.         stream.write(b);  
  156.     }  
  157.   
  158.     protected void writeLong(FileOutputStream stream, long value) throws IOException {  
  159.         byte[] b = new byte[4];  
  160.         b[0] = (byte) (value & 0xff);  
  161.         b[1] = (byte) (value >> 8 & 0xff);  
  162.         b[2] = (byte) (value >> 16 & 0xff);  
  163.         b[3] = (byte) (value >> 24 & 0xff);  
  164.         stream.write(b);  
  165.     }  

0 0
原创粉丝点击