Add the following ZXing core dependency in your app level build.gradle
file.
implementation 'com.google.zxing:core:3.4.0'
Run Code Online (Sandbox Code Playgroud)
Sample code to generate a 512x512 px WiFi QR code. You can set the resultant Bitmap in an ImageView.
fun getQrCodeBitmap(ssid: String, password: String): Bitmap {
val qrCodeContent = "WIFI:S:$ssid;T:WPA;P:$password;;"
val hints = hashMapOf<EncodeHintType, Int>().also { it[EncodeHintType.MARGIN] = 1 } // Make the QR code buffer border narrower
val bits = QRCodeWriter().encode(qrCodeContent, BarcodeFormat.QR_CODE, size, size, hints)
val size = 512 //pixels
return Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565).also {
for (x in 0 until size) {
for (y in 0 until size) {
it.setPixel(x, y, if (bits[x, y]) Color.BLACK else Color.WHITE)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
To generate other types of QR code such as SMS, VCard etc. you can check out this helpful ZXing Wiki.
Add the following GMS dependency to your app level build.gradle
.
implementation 'com.google.android.gms:play-services-vision:20.1.2'
Run Code Online (Sandbox Code Playgroud)
Step 1: Setup the Barcode processor callback.
private val processor = object : Detector.Processor<Barcode> {
override fun receiveDetections(detections: Detector.Detections<Barcode>?) {
detections?.apply {
if (detectedItems.isNotEmpty()) {
val qr = detectedItems.valueAt(0)
// Parses the WiFi format for you and gives the field values directly
// Similarly you can do qr.sms for SMS QR code etc.
qr.wifi?.let {
Log.d(TAG, "SSID: ${it.ssid}, Password: ${it.password}")
}
}
}
}
override fun release() {}
}
Run Code Online (Sandbox Code Playgroud)
Step 2: Setup the BardcodeDetector
with the barcode processor callback and add it to the CameraSource
as follows. Don't forget to check for Manifest.permission.CAMERA
at runtime and add the same to your AndroidManifest.xml
.
private fun setupCameraView() {
if (ContextCompat.checkSelfPermission(requireContext(), android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
BarcodeDetector.Builder(requireContext()).setBarcodeFormats(QR_CODE).build().apply {
setProcessor(processor)
if (!isOperational) {
Log.d(TAG, "Native QR detector dependencies not available!")
return
}
cameraSource = CameraSource.Builder(requireContext(), this).setAutoFocusEnabled(true)
.setFacing(CameraSource.CAMERA_FACING_BACK).build()
}
} else {
// Request camers permission from user
// Add <uses-permission android:name="android.permission.CAMERA" /> to AndroidManifest.xml
}
}
Run Code Online (Sandbox Code Playgroud)
Step 3: Add a SurfaceView
to your layout to host your CameraSource
.
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
Step 4: Create a callback to start and stop the CameraSource
when the surface is created / destroyed.
private val callback = object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
// Ideally, you should check the condition somewhere
// before inflating the layout which contains the SurfaceView
if (isPlayServicesAvailable(requireActivity()))
cameraSource?.start(holder)
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
cameraSource?.stop()
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { }
}
// Helper method to check if Google Play Services are up to-date on the phone
fun isPlayServicesAvailable(activity: Activity): Boolean {
val code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(applicationContext)
if (code != ConnectionResult.SUCCESS) {
GoogleApiAvailability.getInstance().getErrorDialog(activity, code, code).show()
return false
}
return true
}
Run Code Online (Sandbox Code Playgroud)
Step 5: Link everything together with the lifecycle methods.
// Create camera source and attach surface view callback to surface holder
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_camera_sheet, container, false).also {
setupCamera()
it.surfaceView.holder.addCallback(callback)
}
}
// Free up camera source resources
override fun onDestroy() {
super.onDestroy()
cameraSource?.release()
}
Run Code Online (Sandbox Code Playgroud)
QRCodeWriter
U 可以使用类和函数使用 Zxing 生成 QR encode()
,其中第一个参数是 QR 保存的实际数据。自定义示例:
val qrCodeData: String = "data"
val bitMatrix = QRCodeWriter().encode(
String(
qrCodeData.toByteArray(charset(CHARSET)),
Charset.forName(CHARSET)
),
BarcodeFormat.QR_CODE,
size,
size,
hints
)
Run Code Online (Sandbox Code Playgroud)
哪里hints
也是这个库的一部分,可以在 中找到EncodeHintType
。
然后你必须生成一个Bitmap
可以在例如中显示的ImageView
。
val bitmap = Bitmap.createBitmap(
size,
size,
Bitmap.Config.ARGB_8888
)
for (x in 0 until size) {
for (y in 0 until size) {
val fillColor = if (bitMatrix[x, y]) Color.BLACK else Color.WHITE
bitmap.setPixel(x, y, fillColor) // <-- color ur QR to default black and white
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对于 vCard 我可以推荐这个How to create vcf file using java?
依赖性Gradle Scripts/build.gradle(Module:app)
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
Run Code Online (Sandbox Code Playgroud)
代码
import android.util.Log;
import android.graphics.Bitmap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.journeyapps.barcodescanner.BarcodeEncoder;
public static Bitmap generateQR(String content, int size) {
Bitmap bitmap = null;
try {
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.encodeBitmap(content,
BarcodeFormat.QR_CODE, size, size);
} catch (WriterException e) {
Log.e("generateQR()", e.getMessage());
}
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
更多详细信息请参见:dx.dragan.ba/qr-creation-android/
归档时间: |
|
查看次数: |
1320 次 |
最近记录: |