β

安卓-基于ZXing的二维码编码Demo

国祥のBlog 177 阅读

有了上一篇解码,当然就要有编码。其实这篇编码是抄网上的,不过我改了一下,默认编码为UTF-8,这样就支持多国语言文字了。

另附代码:

public Bitmap genQRCODEPicture(String str)
    {
        try
        {
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix matrix = writer.encode(str, BarcodeFormat.QR_CODE, 500,500, hints);

            int w = matrix.getWidth();
            int h = matrix.getHeight();
            int[] rawData = new int[w * h];
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    int color = Color.WHITE;
                    if (matrix.get(i, j))
                    {
                        color = Color.BLACK;
                    }
                    rawData[i + (j * w)] = color;
                }
            }

            Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);
            bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
            return bitmap;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
作者:国祥のBlog
原“国祥的博客”
原文地址:安卓-基于ZXing的二维码编码Demo, 感谢原作者分享。