如何在单个QR码上创建具有多个变量的Qr代码?

时间:2017-10-28 18:35:58

标签: android qr-code

我的应用程序项目具有qr代码生成器功能。我使用Zxing库来编码qr代码生成器,但它只能生成qr代码单个变量。我想在单个qr代码上创建带有3个变量的qr代码。请帮我创建这个。 附:我是Android的新秀。

我的代码

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;


public class MainActivity extends AppCompatActivity {

private EditText editText , editText2 , editText3;
private String qrText1 , qrText2 , qrText3;
private ImageView qrCodeView;

@Override
protected void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   editText = (EditText) this.findViewById(R.id.editText);
   editText2 = (EditText) this.findViewById(R.id.editText2);
   editText3 = (EditText) this.findViewById(R.id.editText3);

   Button generateBtn = (Button) this.findViewById(R.id.generateButton);
   qrCodeView = (ImageView) this.findViewById(R.id.imageView);

   generateBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         qrText1 = editText.getText().toString().trim();
         qrText2 = editText2.getText().toString().trim();
         qrText3 = editText3.getText().toString().trim();

         MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
         try {
            BitMatrix bitMatrix = multiFormatWriter.encode(qrText1, BarcodeFormat.QR_CODE, 200, 200);
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
            qrCodeView.setImageBitmap(bitmap);

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

我不知道如何在创建单个qr代码时使用qrText1添加qrText2和qrText3。

1 个答案:

答案 0 :(得分:0)

generateBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        qrText1 = editText.getText().toString().trim();
        qrText2 = editText2.getText().toString().trim();
        qrText3 = editText3.getText().toString().trim();

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        try {
            BitMatrix bitMatrix = multiFormatWriter.encode("Text1 :"+ qrText1 + "\n 
Text2"+ qrText2 +"\n Text3"+ qrText3 , BarcodeFormat.QR_CODE, 200, 200);
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
            qrCodeView.setImageBitmap(bitmap);
        }
        catch (WriterException e) 
        {
            e.printStackTrace();
        }
    }
});