mirror of
https://github.com/markusfisch/BinaryEye.git
synced 2024-09-20 12:02:17 +02:00
Give EnumMap of encode hints to Zxing.encode()
Instead of transferring just the ErrorCorrectionLevel from the EncodeFragment. This way we can transfer additional hints for other Barcode formats too.
This commit is contained in:
parent
1d156e0dbd
commit
0eb921d579
@ -9,7 +9,7 @@ import android.support.v4.app.Fragment
|
|||||||
import android.view.*
|
import android.view.*
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
|
import com.google.zxing.EncodeHintType
|
||||||
import de.markusfisch.android.binaryeye.R
|
import de.markusfisch.android.binaryeye.R
|
||||||
import de.markusfisch.android.binaryeye.app.*
|
import de.markusfisch.android.binaryeye.app.*
|
||||||
import de.markusfisch.android.binaryeye.view.doOnApplyWindowInsets
|
import de.markusfisch.android.binaryeye.view.doOnApplyWindowInsets
|
||||||
@ -60,14 +60,12 @@ class BarcodeFragment : Fragment() {
|
|||||||
val args = arguments ?: return view
|
val args = arguments ?: return view
|
||||||
val content = args.getString(CONTENT) ?: return view
|
val content = args.getString(CONTENT) ?: return view
|
||||||
val format = args.getSerializable(FORMAT) as BarcodeFormat? ?: return view
|
val format = args.getSerializable(FORMAT) as BarcodeFormat? ?: return view
|
||||||
val ecl = args.getSerializable(
|
val hints = args.getSerializable(HINTS) as EnumMap<EncodeHintType, Any>?
|
||||||
ERROR_CORRECTION_LEVEL
|
|
||||||
) as ErrorCorrectionLevel?
|
|
||||||
val size = args.getInt(SIZE)
|
val size = args.getInt(SIZE)
|
||||||
try {
|
try {
|
||||||
barcodeBitmap = Zxing.encodeAsBitmap(content, format, size, size, ecl)
|
barcodeBitmap = Zxing.encodeAsBitmap(content, format, size, size, hints)
|
||||||
barcodeSvg = Zxing.encodeAsSvg(content, format, size, size, ecl)
|
barcodeSvg = Zxing.encodeAsSvg(content, format, hints)
|
||||||
barcodeTxt = Zxing.encodeAsTxt(content, format, ecl)
|
barcodeTxt = Zxing.encodeAsText(content, format, hints)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
var message = e.message
|
var message = e.message
|
||||||
if (message == null || message.isEmpty()) {
|
if (message == null || message.isEmpty()) {
|
||||||
@ -235,7 +233,7 @@ class BarcodeFragment : Fragment() {
|
|||||||
companion object {
|
companion object {
|
||||||
private const val CONTENT = "content"
|
private const val CONTENT = "content"
|
||||||
private const val FORMAT = "format"
|
private const val FORMAT = "format"
|
||||||
private const val ERROR_CORRECTION_LEVEL = "error_correction_level"
|
private const val HINTS = "hints"
|
||||||
private const val SIZE = "size"
|
private const val SIZE = "size"
|
||||||
private const val MIME_PNG = "image/png"
|
private const val MIME_PNG = "image/png"
|
||||||
private const val MIME_SVG = "image/svg+xmg"
|
private const val MIME_SVG = "image/svg+xmg"
|
||||||
@ -245,13 +243,13 @@ class BarcodeFragment : Fragment() {
|
|||||||
content: String,
|
content: String,
|
||||||
format: BarcodeFormat,
|
format: BarcodeFormat,
|
||||||
size: Int,
|
size: Int,
|
||||||
errorCorrectionLevel: ErrorCorrectionLevel? = null
|
hints: EnumMap<EncodeHintType, Any>? = null
|
||||||
): Fragment {
|
): Fragment {
|
||||||
val args = Bundle()
|
val args = Bundle()
|
||||||
args.putString(CONTENT, content)
|
args.putString(CONTENT, content)
|
||||||
args.putSerializable(FORMAT, format)
|
args.putSerializable(FORMAT, format)
|
||||||
errorCorrectionLevel?.let {
|
hints?.let {
|
||||||
args.putSerializable(ERROR_CORRECTION_LEVEL, it)
|
args.putSerializable(HINTS, it)
|
||||||
}
|
}
|
||||||
args.putInt(SIZE, size)
|
args.putInt(SIZE, size)
|
||||||
val fragment = BarcodeFragment()
|
val fragment = BarcodeFragment()
|
||||||
|
@ -7,12 +7,14 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.*
|
import android.widget.*
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
|
import com.google.zxing.EncodeHintType
|
||||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
|
||||||
import de.markusfisch.android.binaryeye.R
|
import de.markusfisch.android.binaryeye.R
|
||||||
import de.markusfisch.android.binaryeye.app.addFragment
|
import de.markusfisch.android.binaryeye.app.addFragment
|
||||||
import de.markusfisch.android.binaryeye.app.hideSoftKeyboard
|
import de.markusfisch.android.binaryeye.app.hideSoftKeyboard
|
||||||
import de.markusfisch.android.binaryeye.app.prefs
|
import de.markusfisch.android.binaryeye.app.prefs
|
||||||
import de.markusfisch.android.binaryeye.view.setPaddingFromWindowInsets
|
import de.markusfisch.android.binaryeye.view.setPaddingFromWindowInsets
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class EncodeFragment : Fragment() {
|
class EncodeFragment : Fragment() {
|
||||||
private lateinit var formatView: Spinner
|
private lateinit var formatView: Spinner
|
||||||
@ -122,16 +124,15 @@ class EncodeFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun encode() {
|
private fun encode() {
|
||||||
|
val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
|
||||||
val format = writers[formatView.selectedItemPosition]
|
val format = writers[formatView.selectedItemPosition]
|
||||||
val ecl = if (format == BarcodeFormat.QR_CODE) {
|
if (format == BarcodeFormat.QR_CODE) {
|
||||||
arrayListOf(
|
hints[EncodeHintType.ERROR_CORRECTION] = arrayListOf(
|
||||||
ErrorCorrectionLevel.L,
|
ErrorCorrectionLevel.L,
|
||||||
ErrorCorrectionLevel.M,
|
ErrorCorrectionLevel.M,
|
||||||
ErrorCorrectionLevel.Q,
|
ErrorCorrectionLevel.Q,
|
||||||
ErrorCorrectionLevel.H
|
ErrorCorrectionLevel.H
|
||||||
)[errorCorrectionLevel.selectedItemPosition]
|
)[errorCorrectionLevel.selectedItemPosition]
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
}
|
||||||
val size = getSize(sizeBarView.progress)
|
val size = getSize(sizeBarView.progress)
|
||||||
val content = contentView.text.toString()
|
val content = contentView.text.toString()
|
||||||
@ -145,7 +146,7 @@ class EncodeFragment : Fragment() {
|
|||||||
activity?.hideSoftKeyboard(contentView)
|
activity?.hideSoftKeyboard(contentView)
|
||||||
addFragment(
|
addFragment(
|
||||||
fragmentManager,
|
fragmentManager,
|
||||||
BarcodeFragment.newInstance(content, format, size, ecl)
|
BarcodeFragment.newInstance(content, format, size, hints)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ package de.markusfisch.android.binaryeye.zxing
|
|||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import com.google.zxing.*
|
import com.google.zxing.*
|
||||||
|
import com.google.zxing.common.BitMatrix
|
||||||
import com.google.zxing.common.HybridBinarizer
|
import com.google.zxing.common.HybridBinarizer
|
||||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class Zxing(possibleResultPoint: ResultPointCallback? = null) {
|
class Zxing(possibleResultPoint: ResultPointCallback? = null) {
|
||||||
@ -11,7 +11,7 @@ class Zxing(possibleResultPoint: ResultPointCallback? = null) {
|
|||||||
private val multiFormatReader = MultiFormatReader()
|
private val multiFormatReader = MultiFormatReader()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val decodeFormats = EnumSet.noneOf<BarcodeFormat>(
|
val decodeFormats = EnumSet.noneOf(
|
||||||
BarcodeFormat::class.java
|
BarcodeFormat::class.java
|
||||||
)
|
)
|
||||||
decodeFormats.addAll(
|
decodeFormats.addAll(
|
||||||
@ -129,27 +129,16 @@ class Zxing(possibleResultPoint: ResultPointCallback? = null) {
|
|||||||
format: BarcodeFormat,
|
format: BarcodeFormat,
|
||||||
width: Int,
|
width: Int,
|
||||||
height: Int,
|
height: Int,
|
||||||
errorCorrectionLevel: ErrorCorrectionLevel? = null
|
hints: EnumMap<EncodeHintType, Any>? = null
|
||||||
): Bitmap? {
|
): Bitmap? {
|
||||||
val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
|
val bitMatrix = encode(text, format, hints, width, height)
|
||||||
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
|
val w = bitMatrix.width
|
||||||
errorCorrectionLevel?.let {
|
val h = bitMatrix.height
|
||||||
hints[EncodeHintType.ERROR_CORRECTION] = it
|
|
||||||
}
|
|
||||||
val result = MultiFormatWriter().encode(
|
|
||||||
text,
|
|
||||||
format,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
hints
|
|
||||||
)
|
|
||||||
val w = result.width
|
|
||||||
val h = result.height
|
|
||||||
val pixels = IntArray(w * h)
|
val pixels = IntArray(w * h)
|
||||||
var offset = 0
|
var offset = 0
|
||||||
for (y in 0 until h) {
|
for (y in 0 until h) {
|
||||||
for (x in 0 until w) {
|
for (x in 0 until w) {
|
||||||
pixels[offset + x] = if (result.get(x, y)) {
|
pixels[offset + x] = if (bitMatrix.get(x, y)) {
|
||||||
BLACK
|
BLACK
|
||||||
} else {
|
} else {
|
||||||
WHITE
|
WHITE
|
||||||
@ -165,30 +154,17 @@ class Zxing(possibleResultPoint: ResultPointCallback? = null) {
|
|||||||
fun encodeAsSvg(
|
fun encodeAsSvg(
|
||||||
text: String,
|
text: String,
|
||||||
format: BarcodeFormat,
|
format: BarcodeFormat,
|
||||||
width: Int,
|
hints: EnumMap<EncodeHintType, Any>? = null
|
||||||
height: Int,
|
|
||||||
errorCorrectionLevel: ErrorCorrectionLevel? = null
|
|
||||||
): String {
|
): String {
|
||||||
val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
|
val bitMatrix = encode(text, format, hints)
|
||||||
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
|
|
||||||
errorCorrectionLevel?.let {
|
|
||||||
hints[EncodeHintType.ERROR_CORRECTION] = it
|
|
||||||
}
|
|
||||||
val result = MultiFormatWriter().encode(
|
|
||||||
text,
|
|
||||||
format,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
hints
|
|
||||||
)
|
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
val w = result.width
|
val w = bitMatrix.width
|
||||||
var h = result.height
|
var h = bitMatrix.height
|
||||||
val moduleHeight = if (h == 1) w / 2 else 1
|
val moduleHeight = if (h == 1) w / 2 else 1
|
||||||
for (y in 0 until h) {
|
for (y in 0 until h) {
|
||||||
for (x in 0 until w) {
|
for (x in 0 until w) {
|
||||||
if (result.get(x, y)) {
|
if (bitMatrix.get(x, y)) {
|
||||||
sb.append(" M${x},${y}h1v${moduleHeight}h-1z");
|
sb.append(" M${x},${y}h1v${moduleHeight}h-1z")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -196,38 +172,47 @@ class Zxing(possibleResultPoint: ResultPointCallback? = null) {
|
|||||||
return """<svg width="$w" height="$h"
|
return """<svg width="$w" height="$h"
|
||||||
viewBox="0 0 $w $h"
|
viewBox="0 0 $w $h"
|
||||||
xmlns="http://www.w3.org/2000/svg">
|
xmlns="http://www.w3.org/2000/svg">
|
||||||
<path d="${sb.toString()}"/>
|
<path d="$sb"/>
|
||||||
</svg>
|
</svg>
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
fun encodeAsTxt(
|
fun encodeAsText(
|
||||||
text: String,
|
text: String,
|
||||||
format: BarcodeFormat,
|
format: BarcodeFormat,
|
||||||
errorCorrectionLevel: ErrorCorrectionLevel? = null
|
hints: EnumMap<EncodeHintType, Any>? = null
|
||||||
): String {
|
): String {
|
||||||
val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
|
val bitMatrix = encode(text, format, hints)
|
||||||
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
|
val w = bitMatrix.width
|
||||||
errorCorrectionLevel?.let {
|
val h = bitMatrix.height
|
||||||
hints[EncodeHintType.ERROR_CORRECTION] = it
|
|
||||||
}
|
|
||||||
val result = MultiFormatWriter().encode(
|
|
||||||
text,
|
|
||||||
format,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
hints
|
|
||||||
)
|
|
||||||
val w = result.width
|
|
||||||
val h = result.height
|
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
for (y in 0 until h) {
|
for (y in 0 until h) {
|
||||||
for (x in 0 until w) {
|
for (x in 0 until w) {
|
||||||
sb.append(if (result.get(x, y)) "█" else " ")
|
sb.append(if (bitMatrix.get(x, y)) "█" else " ")
|
||||||
}
|
}
|
||||||
sb.append("\n")
|
sb.append("\n")
|
||||||
}
|
}
|
||||||
return sb.toString()
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun encode(
|
||||||
|
text: String,
|
||||||
|
format: BarcodeFormat,
|
||||||
|
encodeHints: EnumMap<EncodeHintType, Any>? = null,
|
||||||
|
width: Int = 0,
|
||||||
|
height: Int = 0
|
||||||
|
): BitMatrix {
|
||||||
|
val hints = encodeHints ?: EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
|
||||||
|
if (!hints.contains(EncodeHintType.CHARACTER_SET)) {
|
||||||
|
hints[EncodeHintType.CHARACTER_SET] = "utf-8"
|
||||||
|
}
|
||||||
|
return MultiFormatWriter().encode(
|
||||||
|
text,
|
||||||
|
format,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
hints
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user