【问题标题】:Empty image when creating from a textview从文本视图创建时为空图像
【发布时间】:2014-07-15 19:56:50
【问题描述】:

我正在尝试使用 zebra api 打印条形码图像。我有我的条形码并且可以成功地在屏幕上显示它,但是我现在正尝试将它保存为 png 并使用热敏打印机打印它。

我的问题是保存的 .png 是空的?谁能指出我正确的方向?

代码:

  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();
                }
            }
        }

更新:

我现在有了这个,但是我的条形码很小!有谁知道如何增加大小以填充创建的框?整个 png 也被打印为黑色,所以条形码不会扫描,因为它已经被反转了?

 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();
                }
            }
        }

【问题讨论】:

  • 您没有向您创建的画布添加任何内容。我建议改为获取绘图缓存。
  • 天啊!感谢将查看绘图缓存文档
  • 应该比较简单,t.setDrawingCacheEnabled(true);t.buildDrawingCache();if(t.getDrawingCache()!=null)//means it is too big Bitmap testB = t.getDrawingCache();
  • 我已经更新了我上面的代码,问题是现在打印的条形码很小,我尝试设置字体大小但没有成功,有什么想法吗?
  • @hypd09,事实证明这一点非常简单,只需更改 textview 的 xml ;-) 如果您想将其作为单独的问题,您的第二条评论是原始问题的答案回答,然后我将其标记为正确。

标签: android textview zebra-printers


【解决方案1】:

提交我的 cmets 作为答案。

您没有向您创建的画布添加任何内容。

我建议改为获取绘图缓存。应该很简单,

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2011-10-05
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    相关资源
    最近更新 更多