android zxing生成二维码

参考网上的二维码

package request.http.com.httprequest.fragment.my;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import request.http.com.httprequest.R;
import request.http.com.httprequest.activity.MainActivity;

/**
 * Date: 2019/7/24
 * Time: 15:15
 * author:Guest
 */
public class pay_receiveFragment extends Fragment implements View.OnClickListener {
    @BindView(R.id.iv_receive_back)
    ImageView iv_receive_back;
    @BindView(R.id.iv_receive_qrcode)
    ImageView iv_receive_qrcode;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View  view = inflater.inflate(R.layout.fragment_pay_receive , container , false);
        Unbinder unbinder = ButterKnife.bind(this, view);
        Message message = new Message();
        message.arg1 = 202;
        MainActivity.mainHandler.sendMessage(message);
        initView();
        initData();
        return view;
    }
    private void  initView(){
        iv_receive_back.setOnClickListener(this);
    }
    private void  initData(){
        QRCode("https://www.baidu.com/",500,500);
    }

    @Override
    public void onClick(View v) {
     switch (v.getId()){
         case R.id.iv_receive_back:
             getActivity()
                     .getSupportFragmentManager()
                     .beginTransaction()
                     .addToBackStack(null)
                     .replace(R.id.fy_fragment,new payFragment())
                     .commit();
             break;
     }
    }
  /**
     * 
     * @param content: 地址,生成二维码地址
     * @param width:二维码的宽度
     * @param height:二维码的高度
     */
    private void QRCode(String content, int width, int height) {
        try {
            QRCodeWriter qrWriter = new QRCodeWriter() ;
            Map map = new HashMap();
            map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix  bitMatrix = qrWriter.encode(content, BarcodeFormat.QR_CODE, width, height,map);
            int[] pixels = new int[width*height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (bitMatrix.get(j, i)) {
                        pixels[i*width+j] = 0xff000000;
                    }else {
                        pixels[i*width+j] = 0xffffffff;
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            iv_receive_qrcode.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Android,二维码)