androidbitmapreceipt

How to generate Bitmap?


I want to write a android receipt program by writing text, setLogo and image into Bitmap. I had search through internet but non of them are relevant to what I'm try to achieve. Is there anyway for me to make it?

My receipt output format is as below:

 _____________
| *********** |
| *  Logo   * | 
| *********** |
| Date        |
| Amount      |
| Card No     |
|             |
| Signature   |
|_____________|

For your information, my purpose is let user review the receipt before terminal print out the receipt. In printReceipt function will do all the generate receipt bitmap job and printPreview function will to set the bitmap into imageView.

Example in real world:- Supermarket receipt, the data will keep increase when we buy a lot of goods, and I'm try to write those data into bitmap and print it out or send through email.


Solution

  • You can convert the layout view directly to a bitmap using below code.

    View v = printPreviewLayout;
    Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    
    Canvas c = new Canvas(bmp);
    v.draw(c);
    
    try{
        File file =  new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        MediaScannerConnection.scanFile(mContext, new String[] { file.toString() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
       });
       Toast.makeText(mContext, "Image downloaded to"+file.toString(), Toast.LENGTH_LONG).show();
    }
    

    This will draw a bitmap of printPreviewLayout view.