Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Barcode Optimization #2735

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ import android.media.Image
import android.os.Build.VERSION.SDK_INT
import android.util.Log
import androidx.annotation.RequiresApi
import com.google.zxing.*
import com.google.zxing.BarcodeFormat
import com.google.zxing.BinaryBitmap
import com.google.zxing.ChecksumException
import com.google.zxing.DecodeHintType
import com.google.zxing.FormatException
import com.google.zxing.LuminanceSource
import com.google.zxing.NotFoundException
import com.google.zxing.PlanarYUVLuminanceSource
import com.google.zxing.RGBLuminanceSource
import com.google.zxing.Result
import com.google.zxing.common.HybridBinarizer
import java.nio.ByteBuffer
import java.nio.IntBuffer
Expand All @@ -21,6 +30,7 @@ private const val TAG = "BarcodeDecodeHelper"
class BarcodeDecodeHelper(formats: List<BarcodeFormat>, multi: Boolean = true) {
private val reader = MultiBarcodeReader(
mapOf(
DecodeHintType.TRY_HARDER to true,
DecodeHintType.ALSO_INVERTED to true,
DecodeHintType.POSSIBLE_FORMATS to formats
)
Expand All @@ -43,22 +53,56 @@ class BarcodeDecodeHelper(formats: List<BarcodeFormat>, multi: Boolean = true) {
}
}

fun decodeFromLuminanceBytes(bytes: ByteArray, width: Int, height: Int, rowStride: Int = width): List<Result> {
return decodeFromSource(PlanarYUVLuminanceSource(bytes, rowStride, height, 0, 0, width, height, false))
fun decodeFromLuminanceBytes(rawBarcodeData: RawBarcodeData, rotate: Int): List<Result> {
Log.d(TAG, "decodeFromLuminanceBytes rotate:")
rawBarcodeData.rotateDetail(rotate)
return decodeFromSource(
PlanarYUVLuminanceSource(
rawBarcodeData.bytes, rawBarcodeData.width, rawBarcodeData.height,
0, 0, rawBarcodeData.width, rawBarcodeData.height, false
)
)
}

fun decodeFromLuminanceBytes(buffer: ByteBuffer, width: Int, height: Int, rowStride: Int = width): List<Result> {
fun decodeFromLuminanceBytes(buffer: ByteBuffer, width: Int, height: Int, rotate: Int = 0): List<Result> {
val bytes = ByteArray(buffer.remaining())
buffer.get(bytes)
buffer.rewind()
return decodeFromLuminanceBytes(bytes, width, height, rowStride)
val rawBarcodeData = RawBarcodeData(bytes, width, height)
return decodeFromLuminanceBytes(rawBarcodeData, rotate)
}

@RequiresApi(19)
fun decodeFromImage(image: Image): List<Result> {
fun decodeFromImage(image: Image, rotate: Int = 0): List<Result> {
if (image.format !in SUPPORTED_IMAGE_FORMATS) return emptyList()
val yPlane = image.planes[0]
return decodeFromLuminanceBytes(yPlane.buffer, image.width, image.height, yPlane.rowStride)
val rawBarcodeData =RawBarcodeData(getYUVBytesFromImage(image), image.width, image.height)
return decodeFromLuminanceBytes(rawBarcodeData, rotate)
}

private fun getYUVBytesFromImage(image: Image): ByteArray {
val planes = image.planes
val width = image.width
val height = image.height
val yuvBytes = ByteArray(width * height * 3 / 2)
var offset = 0

for (i in planes.indices) {
val buffer = planes[i].buffer
val rowStride = planes[i].rowStride
val pixelStride = planes[i].pixelStride
val planeWidth = if ((i == 0)) width else width / 2
val planeHeight = if ((i == 0)) height else height / 2

val planeBytes = ByteArray(buffer.capacity())
buffer[planeBytes]

for (row in 0 until planeHeight) {
for (col in 0 until planeWidth) {
yuvBytes[offset++] = planeBytes[row * rowStride + col * pixelStride]
}
}
}
return yuvBytes
}

fun decodeFromBitmap(bitmap: Bitmap): List<Result> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BarcodeDetector(val context: Context, val options: BarcodeDetectorOptions)
override fun detectBytes(wrappedByteBuffer: IObjectWrapper, metadata: FrameMetadataParcel): Array<Barcode> {
if (!loggedOnce) Log.d(TAG, "detectBytes(${ObjectWrapper.unwrap(wrappedByteBuffer)}, $metadata)").also { loggedOnce = true }
val bytes = wrappedByteBuffer.unwrap<ByteBuffer>() ?: return emptyArray()
return helper.decodeFromLuminanceBytes(bytes, metadata.width, metadata.height)
return helper.decodeFromLuminanceBytes(bytes, metadata.width, metadata.height, metadata.rotation)
.mapNotNull { runCatching { it.toGms(metadata) }.getOrNull() }.toTypedArray()
}

Expand Down Expand Up @@ -224,12 +224,7 @@ private fun Result.toGms(metadata: FrameMetadataParcel): Barcode {
barcode.rawBytes = rawBytes
barcode.rawValue = text
barcode.cornerPoints = resultPoints.map {
when (metadata.rotation) {
1 -> Point(metadata.height - it.y.toInt(), it.x.toInt())
2 -> Point(metadata.width - it.x.toInt(), metadata.height - it.y.toInt())
3 -> Point(it.y.toInt(), metadata.width - it.x.toInt())
else -> Point(it.x.toInt(), it.y.toInt())
}
Point(it.x.toInt(), it.y.toInt())
}.toTypedArray()

val parsed = ResultParser.parseResult(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.microg.gms.vision.barcode

import android.content.Context
import android.graphics.Bitmap
import android.graphics.ImageFormat
import android.graphics.Point
import android.media.Image
Expand Down Expand Up @@ -42,8 +43,9 @@ class BarcodeScanner(val context: Context, val options: BarcodeScannerOptions) :
override fun detect(wrappedImage: IObjectWrapper, metadata: ImageMetadata): List<Barcode> {
if (!loggedOnce) Log.d(TAG, "detect(${ObjectWrapper.unwrap(wrappedImage)}, $metadata)").also { loggedOnce = true }
return when (metadata.format) {
ImageFormat.NV21 -> wrappedImage.unwrap<ByteBuffer>()?.let { helper.decodeFromLuminanceBytes(it, metadata.width, metadata.height) }
ImageFormat.YUV_420_888 -> if (SDK_INT >= 19) wrappedImage.unwrap<Image>()?.let { image -> helper.decodeFromImage(image) } else null
-1 -> wrappedImage.unwrap<Bitmap>()?.let { helper.decodeFromBitmap(it) }
ImageFormat.NV21 -> wrappedImage.unwrap<ByteBuffer>()?.let { helper.decodeFromLuminanceBytes(it, metadata.width, metadata.height, metadata.rotation) }
ImageFormat.YUV_420_888 -> if (SDK_INT >= 19) wrappedImage.unwrap<Image>()?.let { image -> helper.decodeFromImage(image, metadata.rotation) } else null

else -> null
}?.map { it.toMlKit(metadata) } ?: emptyList()
Expand Down Expand Up @@ -230,12 +232,7 @@ private fun Result.toMlKit(metadata: ImageMetadata): Barcode {
barcode.rawBytes = rawBytes
barcode.rawValue = text
barcode.cornerPoints = resultPoints.map {
when (metadata.rotation) {
1 -> Point(metadata.height - it.y.toInt(), it.x.toInt())
2 -> Point(metadata.width - it.x.toInt(), metadata.height - it.y.toInt())
3 -> Point(it.y.toInt(), metadata.width - it.x.toInt())
else -> Point(it.x.toInt(), it.y.toInt())
}
Point(it.x.toInt(), it.y.toInt())
}.toTypedArray()

val parsed = ResultParser.parseResult(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.microg.gms.vision.barcode

import android.util.Log
import android.view.Surface

class RawBarcodeData(var bytes: ByteArray, var width: Int, var height: Int) {

fun rotateDetail(rotate: Int){
when (rotate) {
Surface.ROTATION_90 -> rotateDegree90()
Surface.ROTATION_180 -> rotateDegree180()
Surface.ROTATION_270 -> rotateDegree270()
else -> this
}
}

private fun rotateDegree90(){
val rotatedData = ByteArray(bytes.size)
var index = 0

// Rotate Y plane
for (col in 0 until width) {
for (row in height - 1 downTo 0) {
rotatedData[index++] = bytes[row * width + col]
}
}

// Rotate UV planes (UV interleaved)
val uvHeight = height / 2
for (col in 0 until width step 2) {
for (row in uvHeight - 1 downTo 0) {
rotatedData[index++] = bytes[width * height + row * width + col]
rotatedData[index++] = bytes[width * height + row * width + col + 1]
}
}
bytes = rotatedData
val temp = width
width = height
height = temp
}

private fun rotateDegree180() {
val rotatedData = ByteArray(bytes.size)
var index = 0

// Rotate Y plane
for (row in height - 1 downTo 0) {
for (col in width - 1 downTo 0) {
rotatedData[index++] = bytes[row * width + col]
}
}

// Rotate UV planes (UV interleaved)
val uvHeight = height / 2
val uvWidth = width / 2
for (row in uvHeight - 1 downTo 0) {
for (col in uvWidth - 1 downTo 0) {
val offset = width * height + row * width + col * 2
rotatedData[index++] = bytes[offset]
rotatedData[index++] = bytes[offset + 1]
}
}
bytes = rotatedData
}


private fun rotateDegree270(){
val rotatedData = ByteArray(bytes.size)
var index = 0

// Rotate Y plane
for (col in width - 1 downTo 0) {
for (row in 0 until height) {
rotatedData[index++] = bytes[row * width + col]
}
}

// Rotate UV planes (UV interleaved)
val uvHeight = height / 2
for (col in width - 1 downTo 0 step 2) {
for (row in 0 until uvHeight) {
rotatedData[index++] = bytes[width * height + row * width + col - 1]
rotatedData[index++] = bytes[width * height + row * width + col]
}
}
bytes = rotatedData
val temp = width
width = height
height = temp
}

override fun toString(): String {
return "RawBarcodeData(bytes=${bytes.size}, width=$width, height=$height)"
}
}
Loading