zoukankan      html  css  js  c++  java
  • 二维码

    public class TwoActivity extends Activity implements OnClickListener {
        private int QR_WIDTH = 400;
        private int QR_HEIGHT = 400;
        private ImageView qr_image;
        private Button bt_button;
        private EditText qr_text;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_two);
            qr_text = (EditText) findViewById(R.id.qr_text);
            qr_image = (ImageView) findViewById(R.id.qr_image);
            bt_button = (Button) findViewById(R.id.bt_button);
            bt_button.setOnClickListener(this);

        }

        // 生成QR图
        private void createImage() {
            try {
                // 需要引入core包
                QRCodeWriter writer = new QRCodeWriter();
                String text = qr_text.getText().toString();
                if (text == null || "".equals(text) || text.length() < 1) {
                    return;
                }
                // 把输入的文本转为二维码
                BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
                        QR_WIDTH, QR_HEIGHT);

                System.out.println("w:" + martix.getWidth() + "h:"
                        + martix.getHeight());

                Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                BitMatrix bitMatrix = new QRCodeWriter().encode(text,
                        BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
                int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
                for (int y = 0; y < QR_HEIGHT; y++) {
                    for (int x = 0; x < QR_WIDTH; x++) {
                        if (bitMatrix.get(x, y)) {
                            pixels[y * QR_WIDTH + x] = 0xff000000;
                        } else {
                            pixels[y * QR_WIDTH + x] = 0xffffffff;
                        }

                    }
                }

                Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
                        Bitmap.Config.ARGB_8888);

                bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
                qr_image.setImageBitmap(bitmap);

            } catch (WriterException e) {
                e.printStackTrace();
            }
        }

        public void onClick(View v) {
            createImage();

        }

    }

  • 相关阅读:
    代码可复用性
    开始读《道不远人深入解析ASP.NET 2.0控件开发>>
    我的软件通讯录之二
    .Net中的堆于栈
    JavaScript技巧
    我的软件之通讯录(C#)
    快过年了,自己却病了,哎~~~~~~~
    整合dz论坛短消息出现的问题
    [转]用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上。
    [转]Javascript 调用MSAgent(Desc:网页中出现魔法巫师)
  • 原文地址:https://www.cnblogs.com/fanzhiguo/p/5795160.html
Copyright © 2011-2022 走看看