Generate Aztec code with zxing
This commit is contained in:
parent
b42a25e42e
commit
b67da818c2
|
@ -50,6 +50,7 @@ dependencies {
|
||||||
implementation(libs.androidx.navigation.fragment.ktx)
|
implementation(libs.androidx.navigation.fragment.ktx)
|
||||||
implementation(libs.androidx.navigation.ui.ktx)
|
implementation(libs.androidx.navigation.ui.ktx)
|
||||||
implementation(libs.play.services.code.scanner)
|
implementation(libs.play.services.code.scanner)
|
||||||
|
implementation(libs.core)
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.lhw.pdf
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.Color
|
||||||
|
import com.google.zxing.BarcodeFormat
|
||||||
|
import com.google.zxing.MultiFormatWriter
|
||||||
|
import com.google.zxing.common.BitMatrix
|
||||||
|
|
||||||
|
class BarcodeGenerator {
|
||||||
|
private val bgColor = Color.WHITE
|
||||||
|
private val fgColor = Color.BLACK
|
||||||
|
|
||||||
|
fun createBitmap(bitMatrix: BitMatrix): Bitmap {
|
||||||
|
val width = bitMatrix.width
|
||||||
|
val height = bitMatrix.height
|
||||||
|
val pixels = IntArray(width*height)
|
||||||
|
for (y in 0..<height) {
|
||||||
|
val yW = y*width
|
||||||
|
for (x in 0..<width) {
|
||||||
|
pixels[x+yW] = if (bitMatrix.get(x, y)) fgColor else bgColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun encode(src: String): BitMatrix {
|
||||||
|
return MultiFormatWriter().encode(src, BarcodeFormat.AZTEC, 1, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun encodeBitmap(src: String): Bitmap {
|
||||||
|
return createBitmap(encode(src))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,16 +1,21 @@
|
||||||
package com.lhw.pdf.ui
|
package com.lhw.pdf.ui
|
||||||
|
|
||||||
|
import android.graphics.drawable.BitmapDrawable
|
||||||
|
import android.media.Image
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
import com.google.mlkit.vision.barcode.common.Barcode
|
import com.google.mlkit.vision.barcode.common.Barcode
|
||||||
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
|
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
|
||||||
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
|
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
|
||||||
|
import com.lhw.pdf.BarcodeGenerator
|
||||||
import com.lhw.pdf.R
|
import com.lhw.pdf.R
|
||||||
import com.lhw.pdf.databinding.FragmentNetworkBinding
|
import com.lhw.pdf.databinding.FragmentNetworkBinding
|
||||||
import com.lhw.pdf.databinding.FragmentSlideshowBinding
|
import com.lhw.pdf.databinding.FragmentSlideshowBinding
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
class NetworkFragment : Fragment() {
|
class NetworkFragment : Fragment() {
|
||||||
private var _binding: FragmentNetworkBinding? = null
|
private var _binding: FragmentNetworkBinding? = null
|
||||||
|
@ -23,6 +28,17 @@ class NetworkFragment : Fragment() {
|
||||||
val root: View = binding.root
|
val root: View = binding.root
|
||||||
|
|
||||||
binding.btnScanQrCode.setOnClickListener { getQrCode() }
|
binding.btnScanQrCode.setOnClickListener { getQrCode() }
|
||||||
|
val size = 800 //min(binding.linearLayout.width, binding.linearLayout.height)
|
||||||
|
val qrBitmap = BarcodeGenerator().encodeBitmap("pretend I encoded an //ip address:port in here")
|
||||||
|
val drawable = BitmapDrawable(resources, qrBitmap).apply { isFilterBitmap = false }
|
||||||
|
val imageView = ImageView(context).apply {
|
||||||
|
setImageDrawable(drawable)
|
||||||
|
scaleType = ImageView.ScaleType.FIT_CENTER
|
||||||
|
minimumWidth = size
|
||||||
|
minimumHeight = size
|
||||||
|
maxHeight = size
|
||||||
|
}
|
||||||
|
binding.linearLayout.addView(imageView)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,17 +5,25 @@
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".ui.NetworkFragment">
|
tools:context=".ui.NetworkFragment">
|
||||||
|
|
||||||
<!-- TODO: Update blank fragment layout -->
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
|
android:id="@+id/linear_layout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:text="Network fragment"
|
android:orientation="vertical">
|
||||||
android:textAlignment="center" />
|
|
||||||
|
|
||||||
<Button
|
<TextView
|
||||||
android:id="@+id/btn_scan_qr_code"
|
android:layout_width="wrap_content"
|
||||||
android:layout_width="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:text="Network fragment"
|
||||||
android:text="Scan QR Code" />
|
android:textAlignment="center" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_scan_qr_code"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Scan QR Code" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</FrameLayout>
|
</FrameLayout>
|
|
@ -14,6 +14,7 @@ navigationFragmentKtx = "2.7.7"
|
||||||
navigationUiKtx = "2.7.7"
|
navigationUiKtx = "2.7.7"
|
||||||
pdfboxAndroid = "2.0.27.0"
|
pdfboxAndroid = "2.0.27.0"
|
||||||
playServicesCodeScanner = "16.1.0"
|
playServicesCodeScanner = "16.1.0"
|
||||||
|
core = "3.5.3"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||||
|
@ -29,6 +30,7 @@ androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navi
|
||||||
androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigationUiKtx" }
|
androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigationUiKtx" }
|
||||||
pdfbox-android = { module = "com.tom-roush:pdfbox-android", version.ref = "pdfboxAndroid" }
|
pdfbox-android = { module = "com.tom-roush:pdfbox-android", version.ref = "pdfboxAndroid" }
|
||||||
play-services-code-scanner = { group = "com.google.android.gms", name = "play-services-code-scanner", version.ref = "playServicesCodeScanner" }
|
play-services-code-scanner = { group = "com.google.android.gms", name = "play-services-code-scanner", version.ref = "playServicesCodeScanner" }
|
||||||
|
core = { group = "com.google.zxing", name = "core", version.ref = "core" }
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|
Loading…
Reference in New Issue