Search code examples
androidtextviewzebra-printers

Empty image when creating from a textview


I ma trying to print a bar-code image using the zebra api. I have got my bar-code and can successfully display it on screen, however I am trying to now save it as a png and print it using a thermal printer.

The issue I have is that the saved .png is empty? Could anyone point me in the right direction?

Code:

  ZebraPrinter printer = null;
        FileOutputStream fos = null;
        printBarcode pB = new printBarcode();
        EAN13CodeBuilder bb = new EAN13CodeBuilder("124958761310");
        TextView t = (TextView) findViewById(R.id.textView);
        Typeface tf = gettypeFace(this, "EAN-13.ttf");
        t.setTypeface(tf);
        t.setText(bb.getCode());
        Bitmap testB;

        testB = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(testB);

        try
        {
            fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/barcode.png");
            testB.compress(Bitmap.CompressFormat.PNG, 100, fos);

            fos.flush();
            fos.close();
            fos = null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (fos != null)
            {
                try
                {
                    fos.close();
                    fos = null;
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

Update:

I now have this, however my barcode is tiny! anyone know how to increase the size to fill the box created? Also the whole png is being printed as black so the barcode will not scan as it has been inverted?

 ZebraPrinter printer;
        FileOutputStream fos = null;
        printBarcode pB = new printBarcode();
        EAN13CodeBuilder bb = new EAN13CodeBuilder("124958761310");
        TextView t = (TextView) findViewById(R.id.textView);
        Typeface tf = gettypeFace(this, "EAN-13.ttf");
        t.setTypeface(tf);
        String s = "<font size =\"20\">" + bb.getCode() + "</font>";
        t.setText(Html.fromHtml(s));


        t.layout(0,0,400,200);
        t.setDrawingCacheEnabled(true);
        t.buildDrawingCache();
        Bitmap testB  = t.getDrawingCache();

        try
        {
            fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/barcode.png");
            testB.compress(Bitmap.CompressFormat.PNG, 100, fos);

            fos.flush();
            fos.close();
            fos = null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

Solution

  • Submitting my comments as an answer.

    You are not adding anything to the canvas you created.

    I'd suggest getting the drawing cache instead though. Should be fairly simple,

    t.setDrawingCacheEnabled(true); 
    t.buildDrawingCache(); 
    t.setDrawingCacheBackgroundColor(Color.WHITE) //or some other color
    if(t.getDrawingCache()!=null) //null means it is too big     
        Bitmap testB = t.getDrawingCache();