生成二维码

来源:互联网 发布:软件开发北京 编辑:程序博客网 时间:2024/06/05 16:49



首先导入Zxing jar包



这是xml布局


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.erweima.MainActivity">    <Button        android:id="@+id/bt1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="生成二维码"/>    <Button        android:id="@+id/bt2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="生成二维码2"/>    <ImageView        android:id="@+id/imag"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
这是Activity需要写的
public class MainActivity extends AppCompatActivity implements View.OnClickListener {    /**     * 生成二维码     */    private Button mBt1;    /**     * 生成二维码2     */    private Button mBt2;    private ImageView mImag;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        mBt1 = (Button) findViewById(R.id.bt1);        mBt1.setOnClickListener(this);        mBt2 = (Button) findViewById(R.id.bt2);        mBt2.setOnClickListener(this);        mImag = (ImageView) findViewById(R.id.imag);    }    @Override    public void onClick(View v) {        Bitmap bitmap = null;        switch (v.getId()) {            case R.id.bt1:                bitmap = generateBitmap("史少斌", 400, 400);                mImag.setImageBitmap(bitmap);                break;            case R.id.bt2:                bitmap = generateBitmap("http://www.zhuoku.com/zhuomianbizhi/jingxuan-jingxuantaotu/20131004212152%2896%29.htm#turn", 400, 400);                //添加logo                //先得到一个logo图标                Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);                //把logo添加到二维码上                Bitmap customBitmap = addLogo(bitmap, logoBitmap);                mImag.setImageBitmap(customBitmap);                break;        }    }    //生成二维码    private Bitmap generateBitmap(String content, int width, int height) {        QRCodeWriter qrCodeWriter = new QRCodeWriter();        Map<EncodeHintType, String> hints = new HashMap<>();        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");        try {            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);            int[] pixels = new int[width * height];            for (int i = 0; i < height; i++) {                for (int j = 0; j < width; j++) {                    if (encode.get(j, i)) {                        pixels[i * width + j] = 0x00000000;                    } else {                        pixels[i * width + j] = 0xffffffff;                    }                }            }            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);        } catch (WriterException e) {            e.printStackTrace();        }        return null;    }    /**     * 添加图标     *     * @param qrBitmap     * @param logoBitmap     * @return     */    private Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) {        int qrBitmapWidth = qrBitmap.getWidth();        int qrBitmapHeight = qrBitmap.getHeight();        int logoBitmapWidth = logoBitmap.getWidth();        int logoBitmapHeight = logoBitmap.getHeight();        Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(blankBitmap);        canvas.drawBitmap(qrBitmap, 0, 0, null);        canvas.save(Canvas.ALL_SAVE_FLAG);        float scaleSize = 1.0f;        while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) {            scaleSize *= 2;        }        float sx = 1.0f / scaleSize;        canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2);        canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null);        canvas.restore();        return blankBitmap;    }}

原创粉丝点击