将png转换为单色bmp - android

时间:2015-06-29 05:43:17

标签: android canvas png bmp monochrome

我正在开发一个应用程序,在其中我在画布上绘制黑色n白色绘图并以png格式保存

现在我买了一台pos打印机,我需要用单色bmp格式绘图 ,如何在sd卡上保存canvas作为单色bmp或者在读取png文件时将其作为单色bmp保存?

这是我在sd卡上保存画布的代码

Bitmap returnedBitmap = Bitmap.createBitmap(mContent.getWidth(),
            mContent.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);

//  Drawable bgDrawable = mContent.getBackground();

    canvas.drawColor(Color.TRANSPARENT);
    mContent.draw(canvas);




    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root);    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "abc.bmp";

    File file = new File (myDir, fname);
    Log.d("SingatureView",file.getAbsolutePath().toString());


    if (file.exists ()) file.delete (); 
    try {

           FileOutputStream out = new FileOutputStream(file);
//     returnedBitmap = Bitmap.createScaledBitmap(returnedBitmap, 50, 30, false);
           returnedBitmap.compress(Bitmap.CompressFormat.PNG, 0, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }

并阅读文件

String filePath = Environment.getExternalStorageDirectory()+File.separator+"abc.bmp";


   File file = new File(filePath);
   int size = (int) file.length();
   byte[] bytes = new byte[size];
   try {
       BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
       buf.read(bytes, 0, bytes.length);
       buf.close();
   } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   }
  printer.PrintBmp(bytes, bytes.length, 0, 0);

0 个答案:

没有答案