生成二维码和扫描二维码

一、生成二维码

1、布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
>
    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成二维码"/>
    <Button
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成自定义二维码"/>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
</LinearLayout>
2、Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt1= (Button) findViewById(R.id.bt1);
        Button bt2= (Button) findViewById(R.id.bt2);     
        iv = (ImageView) findViewById(R.id.iv);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);      
    }

    @Override
    public void onClick(View v) {
        Bitmap bitmap=null;
    switch (v.getId()){
        case R.id.bt1:
            bitmap=getBitmap("http://www.360kan.com/dianying/index.html",400,400);
            iv.setImageBitmap(bitmap);
            break;
        case R.id.bt2:
            bitmap=getBitmap("http://www.360kan.com/dianying/index.html",400,400);
            Bitmap logoBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
            Bitmap bitmap1=getLogoBitmap(bitmap,logoBitmap);
            iv.setImageBitmap(bitmap1);
            break;
    }
    }

    private Bitmap getLogoBitmap(Bitmap bitmap, Bitmap logoBitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int logoBitmapWidth = logoBitmap.getWidth();
        int logoBitmapHeight = logoBitmap.getHeight();
        Bitmap cusbitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(cusbitmap);
        canvas.drawBitmap(bitmap,0,0,null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        float scaleSize = 1.0f;
        while ((logoBitmapWidth / scaleSize) > (width / 5) || (logoBitmapHeight / scaleSize) > (height / 5)) {
            scaleSize *= 2;
        }
        float sx = 1.0f / scaleSize;
        canvas.scale(sx, sx, width / 2, height / 2);
        canvas.drawBitmap(logoBitmap, (width - logoBitmapWidth) / 2, (height - logoBitmapHeight) / 2, null);
        canvas.restore();

        return cusbitmap;
    }

    private Bitmap getBitmap(String content, int width, int height) {
        QRCodeWriter qrCodeWriter=new QRCodeWriter();
        Map<EncodeHintType,String> hint=new HashMap<>();
        hint.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 (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
二、扫描二维码

文档

http://note.youdao.com/noteshare?id=9f3c12f4303e5183c34bfcf27d8c0319


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值