Reputation: 173
I am developing an application in which I draw black n white drawing on canvas and save it in png format
now I bought a pos printer for which I need drawing in monochrome bmp format , how can I save canvas as monochrome bmp on sd card or while reading read png file as monochrome bmp?
here is my code to save canvas on sd card
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();
}
and to read file
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);
Upvotes: 1
Views: 884