/**  * 把一个View的对象转换成bitmap  */static Bitmap getViewBitmap(View v) {v.clearFocus();v.setPressed(false);//能画缓存就返回false boolean willNotCache = v.willNotCacheDrawing();v.setWillNotCacheDrawing(false);int color = v.getDrawingCacheBackgroundColor();v.setDrawingCacheBackgroundColor(0);if (color != 0) {v.destroyDrawingCache();}v.buildDrawingCache();Bitmap cacheBitmap = v.getDrawingCache();if (cacheBitmap == null) {Log.e("BtPrinter", "failed getViewBitmap(" + v + ")", new RuntimeException());return null;}Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);// Restore the view v.destroyDrawingCache();v.setWillNotCacheDrawing(willNotCache);v.setDrawingCacheBackgroundColor(color);return bitmap;}/*** 将彩色图转换为黑白图* * @param 位图* @return 返回转换好的位图*/public static Bitmap convertToBlackWhite(Bitmap bmp) {int width = bmp.getWidth(); // 获取位图的宽int height = bmp.getHeight(); // 获取位图的高int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组bmp.getPixels(pixels, 0, width, 0, 0, width, height);int alpha = 0xFF << 24;for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {int grey = pixels[width * i + j];int red = ((grey & 0x00FF0000) >> 16);int green = ((grey & 0x0000FF00) >> 8);int blue = (grey & 0x000000FF);grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);grey = alpha | (grey << 16) | (grey << 8) | grey;pixels[width * i + j] = grey;}}Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);newBmp.setPixels(pixels, 0, width, 0, 0, width, height);Bitmap resizeBmp = ThumbnailUtils.extractThumbnail(newBmp, 380, 460);return resizeBmp;}/** * 将Bitmap存为 .bmp格式图片 * @param bitmap */private void saveBmp(Bitmap bitmap) {if (bitmap == null)return;// 位图大小int nBmpWidth = bitmap.getWidth();int nBmpHeight = bitmap.getHeight();// 图像数据大小int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);try {// 存储文件名String filename = "/sdcard/test.bmp";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;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);for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol)for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 3) {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);}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);}


更多相关文章

  1. Android键盘模式android:windowSoftInputMode
  2. android项目--超级简单的android通讯录
  3. Android也谈android和多屏幕适配
  4. Android 缓存 - LruCache
  5. Android 中,应用程序需要的图片资源如何针对不同屏幕大小手机设计
  6. Android 适配不同的屏幕

随机推荐

  1. android异步更新UI
  2. Android——消息机制
  3. 基于rtmp协议流媒体开发值得参考文章
  4. 【Android】Android SDK下载和更新失败的
  5. Android如何发邮件?
  6. 基于Android Volley的网络请求工具
  7. 关于android WebViewClient和WebChromeCl
  8. Android wakeLock 分析
  9. Android(安卓)使用OpenSSL进行3DES加密 c
  10. 关于android内核移植到YLP2440开发板