OS: macOS HighSierra Android Studio: 3.0.1
・ API level setting
build.gradle
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
}
}
Library to use: zxing-android-embedded (3.6.0) https://github.com/journeyapps/zxing-android-embedded
How to add to the project: Add the following sentence to build.gradle (Module: app).
build.gradle
dependencies {
...
compile 'com.journeyapps:zxing-android-embedded:3.6.0'
}
python
new IntentIntegrator(MainActivity.this).initiateScan();
Write the above code where you want to read.
Change the part of MainActivity.this
to the target Activity name and use it.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
Log.d("readQR", result.getContents());
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
The read data can be received as a String type with getContents ()
.
python
//Character string to be QR coded
String data = "https://www.google.com";
//Specify the size of the QR code image(pixel)
int size = 500;
try {
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
//Create QR code with Bitmap
Bitmap bitmap = barcodeEncoder.encodeBitmap(data,BarcodeFormat.QR_CODE, size, size);
//Place the created QR code on the screen
ImageView imageViewQrCode = (ImageView) findViewById(R.id.imageView);
imageViewQrCode.setImageBitmap(bitmap);
} catch (WriterException e) {
throw new AndroidRuntimeException("Barcode Error.", e);
}
-The contents of data
are reflected in the QR code.
-Specify the size of the image with size
.
Prepare ImageView in the xml file corresponding to Acticity, Place the QR code created by Bitmap and display it on the screen.
** <If you want to specify the character code, error correction level, QR code version, etc.> **
python
String data = "https://www.google.com"; //Character string to be QR coded
//Specify the size of the QR code image(pixel)
int size = 500;
try {
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
HashMap hints = new HashMap();
//Character code specification
hints.put(EncodeHintType.CHARACTER_SET, "shiftjis");
//Specify error correction level
//L 7%Can be restored
//M 15%Can be restored
//Q 25%Can be restored
//H 30%Can be restored
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//Specify the QR code version
hints.put(EncodeHintType.QR_VERSION, 20);
Bitmap bitmap = barcodeEncoder.encodeBitmap(data, BarcodeFormat.QR_CODE, size, size, hints);
ImageView imageViewQrCode = (ImageView) findViewById(R.id.imageView);
imageViewQrCode.setImageBitmap(bitmap);
} catch (WriterException e) {
throw new AndroidRuntimeException("Barcode Error.", e);
}
ʻEncode HintType` can be used to create a QR code with detailed specifications.
Recommended Posts