-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
48 lines (37 loc) · 1.53 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.qrcode;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText=findViewById(R.id.edit_text);
Button button=findViewById(R.id.button);
ImageView imageview=findViewById(R.id.qr_code);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MultiFormatWriter multiFormatWriter=new MultiFormatWriter();
try{
BitMatrix bitMatrix=multiFormatWriter.encode(editText.getText().toString(), BarcodeFormat.QR_CODE,600,600);
BarcodeEncoder barcodeEncoder=new BarcodeEncoder();
Bitmap bitmap=barcodeEncoder.createBitmap(bitMatrix);
imageview.setImageBitmap(bitmap);
}catch(WriterException e){
throw new RuntimeException(e);
}
}
});
}
}