0

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.

Nicholas
  • 15
  • 6
  • you have a specific layout.xml for the reciept review right? where you show it to the user before the print. – Anjana Jun 14 '19 at 03:18
  • @Anjana Yup, there will be a printPreview.xml and bitmap will set into ImageView. User can decide whether to print it out through terminal or send through mail. – Nicholas Jun 14 '19 at 03:44
  • So what if you can generate a bitmap from the printPreview layout, would that solve your problem? – Anjana Jun 14 '19 at 03:46
  • @Anjana I guess I understand what you're trying to say, is almost like a template for receipt and I just have to capture the whole template and store it as Bitmap right? – Nicholas Jun 14 '19 at 04:04
  • Simply i am saying is about converting the printPreview layout view to an image. – Anjana Jun 14 '19 at 04:10
  • @Anjana Oh thank, it might be an alternate way, I will look into this solution, see whether is there any view that able to let me handle the large data input (Example, supermarket long receipt), thanks! – Nicholas Jun 14 '19 at 04:35
  • check the suggested ans below. – Anjana Jun 14 '19 at 05:07

3 Answers3

1

I guess you have written the wrong heading. If you want to design a Receipt then you have to design it using the XML Layout Design. For your receipt I have this design.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:padding="16dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    <ImageView
            android:id="@+id/imageview"
            android:layout_centerHorizontal="true"
            android:background="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TableLayout
            android:stretchColumns="*"
            android:layout_marginTop="20dp"
            android:layout_below="@id/imageview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <TableRow>

            <TextView
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    android:text="Date"
            />

            <TextView
                    android:id="@+id/text_view_date"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    tools:text="14/06/2019"
            />

        </TableRow>

        <TableRow>

            <TextView
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    android:text="Amount"
            />

            <TextView
                    android:id="@+id/text_view_amount"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    tools:text="$120"
            />

        </TableRow>

        <TableRow>

            <TextView
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    android:text="Card Number"
            />

            <TextView
                    android:id="@+id/text_view_card_number"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    tools:text="4587 8983 7456 2233"
            />

        </TableRow>

        <TableRow>

            <TextView
                    android:textAppearance="@style/TextAppearance.AppCompat.Large"
                    android:text="Signature"
            />

            <ImageView
                    android:background="@mipmap/ic_launcher"
            />

        </TableRow>


    </TableLayout>

</RelativeLayout>

This XML you will see like this

enter image description here

Now you can add the values to your textviews. And then if you want to convert this screen into a Bitmap you can take the screenshot.

Hope this helps.

Belal Khan
  • 2,099
  • 2
  • 22
  • 32
  • Thanks for the answer and title correction remind, but it's not what I'm try to achieve. – Nicholas Jun 14 '19 at 03:14
  • Then what you are trying to achieve.. explain in detail what exactly you want to do. – Belal Khan Jun 14 '19 at 04:03
  • I understand your solution provide, whole xml will act as a receipt. But I wonder is there any solution to directly draw all the data into bitmap, instead of capture it and store as bitmap. That's what I'm trying to achieve because my data will keep increasing, that's why I try not to fix it in xml and use the way of screenshot, there will be the problem when my data has exceeded the screen resolution I guess screenshot would not be able to capture the receipt that out of screen resolution. Imagine you buy alot of goods in supermarket, and the receipt will be very long right. – Nicholas Jun 14 '19 at 04:22
  • You are thinking about a very wrong approach. You can generate XML from the code as well, so doesn't matter whatever amount of data you have, even if you have variable data, you can code and generate XML dynamically. Drawing a BITMAP is also possible, but it is a very bad idea indeed and you need to write very complex code, dealing with canvas, shapes and text. – Belal Khan Jun 14 '19 at 04:41
0

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.

Anjana
  • 903
  • 1
  • 5
  • 13
0

You can turn your View into a Bitmap.

From the answer here Android Convert view to BitMap

private Bitmap createBitmapFromView(Context context, View view) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);

        return bitmap;
    }

But if you really want to create a Bitmap from scratch. Here is a library that can draw text and image into an image data(byte array) which you can turn into a Bitmap. Graphics 2D. Here's a basic usage of it Convert text content to Image

Israel dela Cruz
  • 794
  • 1
  • 5
  • 11