我想制作一个Android的QR码生成器,但不使用EditText和Button

时间:2018-11-23 04:00:23

标签: java android android-studio generator qr-code

以前我想为自己的无知道歉,这是我做的一些事情,我制作了一个不使用EditText的qrcode生成器,也不使用Button输入字符串,我希望能够通过电话号码来创建一个qrcode在我创建的活动配置文件上,但是当我运行qrcode时,我仅包含单词“ false”,再次为无知而道歉,我会stackoverflow,因为我是这里的新用户。 xml中的(R.id.phone)。我的个人资料,我尝试使用其他xml,谢谢大家,-

public class MyQrcodeActivity extends AppCompatActivity {
private String qrCodeData;
public TextView vPhone;
private User user = null;

@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_qrcode);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("My QR");
    try {
        qrCodeData = getResources().getResourceName(R.id.phone);
    } catch (Exception e) {
        e.printStackTrace();
    }
    qrGenerator();
}

public void qrGenerator() {
    try {
        //setting size of qr code
        WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        int width = point.x;
        int height = point.y;
        int smallestDimension = width < height ? width : height;
        //setting parameters for qr code
        String charset = "UTF-8"; // or "ISO-8859-1"
        Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        createQRCode(qrCodeData, charset, hintMap, smallestDimension, smallestDimension);

    } catch (Exception ex) {
        Log.e("QrGenerate", ex.getMessage());
    }
}

public void createQRCode(String qrCodeData, String charset, Map hintMap, int qrCodeheight, int qrCodewidth) {

    try {
        //generating qr code in bitmatrix type
        BitMatrix matrix = new MultiFormatWriter().encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
        //converting bitmatrix to bitmap
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = matrix.get(x, y) ? BLACK : WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        //setting bitmap to image view
        ImageView myImage = (ImageView) findViewById(R.id.imageViewBitmap);
        myImage.setImageBitmap(bitmap);
    } catch (Exception er) {
        Log.e("QrGenerate", er.getMessage());
    }
}

@Override
public boolean onOptionsItemSelected (MenuItem item){
    int id = item.getItemId();
    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:1)

在您的xml中添加ImageView:

<ImageView
            android:id="@+id/qrcode"
            android:layout_width="@dimen/sdp200"
            android:layout_height="@dimen/sdp200"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="@dimen/margin10"
            android:layout_below="@+id/relativeLayout2"
            android:contentDescription="QR Code"/>

在您的活动中:

ImageView qrcode = findViewById(R.id.qrcode); 
TextView txtnumber = findViewById(R.id.txtmobile);

String strMobile = txtnumber.getText().toString();

Bitmap lBitmap = QRCode.from(strMobile ).bitmap();
qrcode.setImageBitmap(lBitmap);

添加依赖项:

implementation 'com.github.kenglxn.QRGen:android:2.2.0'    

enter image description here

它将生成手机号码的二维码。

希望这会对您有所帮助。