मैं अपने ऐप से कुछ क्यूआर जेनरेट करने की कोशिश कर रहा हूं, लेकिन मैंने देखा है कि संपर्क, वाई-फाई इत्यादि जैसे कई प्रकार के क्यूआर हैं। और मैं सोच रहा हूं कि इसे लागू करने के लिए कोई मुफ्त एपीआई या लाइब्रेरी है या नहीं, मैंने देखा है कि ऐसी वेबसाइटें हैं जो इसे पहले से ही उत्पन्न कर रही हैं इसलिए मैं जानना चाहता था कि एंड्रॉइड के लिए कोई एपीआई या लाइब्रेरी है या नहीं।
मैंने जो चेक किया है:
लेकिन मुझे यकीन नहीं है कि ठीक कहने के लिए कोई फ़ंक्शन है या नहीं मुझे संपर्क के लिए एक क्यूआर चाहिए ताकि मैं इसकी सारी जानकारी जोड़ सकूं।
3 जवाब
ZXing . का उपयोग करके QR कोड जनरेशन
अपने ऐप स्तर build.gradle
फ़ाइल में निम्नलिखित ZXing कोर निर्भरता जोड़ें।
implementation 'com.google.zxing:core:3.4.0'
512x512 px वाईफाई क्यूआर कोड जनरेट करने के लिए नमूना कोड। आप परिणामी बिटमैप को एक छवि दृश्य में सेट कर सकते हैं।
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)
}
}
}
}
अन्य प्रकार के QR कोड जैसे SMS, VCard आदि उत्पन्न करने के लिए आप इस उपयोगी ZXing को देख सकते हैं विकी.
Google मोबाइल विज़न API का उपयोग करके QR कोड स्कैन करना
निम्न GMS निर्भरता को अपने ऐप्लिकेशन स्तर build.gradle
में जोड़ें।
implementation 'com.google.android.gms:play-services-vision:20.1.2'
चरण 1: बारकोड प्रोसेसर कॉलबैक सेट करें।
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() {}
}
चरण 2: BardcodeDetector
को बारकोड प्रोसेसर कॉलबैक के साथ सेटअप करें और इसे CameraSource
में निम्नानुसार जोड़ें। रनटाइम पर Manifest.permission.CAMERA
की जांच करना न भूलें और इसे अपने 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
}
}
चरण 3: अपने CameraSource
को होस्ट करने के लिए अपने लेआउट में एक SurfaceView
जोड़ें।
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
चरण 4: सतह के बनने/नष्ट होने पर CameraSource
को प्रारंभ और बंद करने के लिए एक कॉलबैक बनाएं।
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
}
चरण 5: जीवनचक्र विधियों के साथ सब कुछ एक साथ जोड़ें।
// 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()
}
यदि आप zxing-android-embedded
का उपयोग कर रहे हैं
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}
आप इसे छोटा कर सकते हैं
val barcodeEncoder = BarcodeEncoder()
val bitmap = barcodeEncoder.encodeBitmap(content, BarcodeFormat.QR_CODE, 512, 512)
your_image_view.setImageBitmap(bitmap)
U QRCodeWriter
वर्ग का उपयोग करके 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
)
जहां 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
}
}
implementation 'com.google.zxing:core:3.4.0'
में Zxing के लिए निर्भरता जोड़नी चाहिए
"WIFI:S:$ssid;T:WPA;P:$passkey;;"
है। इसी तरह आप संपर्कों आदि के लिए तार ढूंढ सकते हैं। बीटीडब्ल्यू यह वाईफाई कोड Google मोबाइल विजन बारकोड एपीआई द्वारा भी स्ट्रिंग को मैन्युअल रूप से विभाजित किए बिना समझा जाता है।
संबंधित सवाल
नए सवाल
android
एंड्रॉइड Google का मोबाइल ऑपरेटिंग सिस्टम है, जिसका उपयोग प्रोग्रामिंग या डिजिटल डिवाइस (स्मार्टफोन, टैबलेट, ऑटोमोबाइल्स, टीवी, वियर, ग्लास, IoT) को विकसित करने के लिए किया जाता है। एंड्रॉइड से संबंधित विषयों के लिए, एंड्रॉइड-विशिष्ट टैग जैसे कि एंड्रॉइड-इरादे, एंड्रॉइड-गतिविधि, एंड्रॉइड-एडॉप्टर आदि का उपयोग करें। विकास या प्रोग्रामिंग के अलावा अन्य प्रश्नों के लिए, लेकिन एंड्रॉइड फ्रेमवर्क से संबंधित हैं, इस लिंक का उपयोग करें: https: // android.stackexchange.com।