Android: Converting a Bitmap to a Monochrome Bitmap (1 Bit per Pixel)

前端 未结 4 1587
一向
一向 2020-12-05 00:57

I want to print a Bitmap to a mobile Bluetooth Printer (Bixolon SPP-R200) - the SDK doesn\'t offer direkt methods to print an in-memory image. So I thought about converting

4条回答
  •  星月不相逢
    2020-12-05 01:18

    You should convert each pixel into HSV space and use the value to determine if the Pixel on the target image should be black or white:

      Bitmap bwBitmap = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565 );
      float[] hsv = new float[ 3 ];
      for( int col = 0; col < bitmap.getWidth(); col++ ) {
        for( int row = 0; row < bitmap.getHeight(); row++ ) {
          Color.colorToHSV( bitmap.getPixel( col, row ), hsv );
          if( hsv[ 2 ] > 0.5f ) {
            bwBitmap.setPixel( col, row, 0xffffffff );
          } else {
            bwBitmap.setPixel( col, row, 0xff000000 );
          }
        }
      }
      return bwBitmap;
    

提交回复
热议问题