Apply Kotlin Coding Conventions

From:
https://kotlinlang.org/docs/reference/coding-conventions.html
This commit is contained in:
Markus Fisch
2018-02-27 14:22:41 +01:00
parent a6449b50e9
commit 279c988464
9 changed files with 350 additions and 260 deletions
@@ -30,12 +30,6 @@ import android.widget.SeekBar
import android.widget.Toast
class CameraActivity : AppCompatActivity() {
companion object {
private val REQUEST_CAMERA = 1
private val ZOOM_MAX = "zoom_max"
private val ZOOM_LEVEL = "zoom_level"
}
private val zxing = Zxing()
private val decodingRunnable = Runnable {
while (decoding) {
@@ -64,14 +58,18 @@ class CameraActivity : AppCompatActivity() {
private var flash = false
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray) {
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
when (requestCode) {
REQUEST_CAMERA -> if (grantResults.size > 0 &&
grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.no_camera_no_fun,
Toast.LENGTH_SHORT).show()
grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(
this,
R.string.no_camera_no_fun,
Toast.LENGTH_SHORT
).show()
finish()
}
}
@@ -113,8 +111,11 @@ class CameraActivity : AppCompatActivity() {
super.onResume()
System.gc()
if (hasCameraPermission()) {
cameraView.openAsync(CameraView.findCameraId(
Camera.CameraInfo.CAMERA_FACING_BACK))
cameraView.openAsync(
CameraView.findCameraId(
Camera.CameraInfo.CAMERA_FACING_BACK
)
)
startDecoding()
}
}
@@ -157,13 +158,18 @@ class CameraActivity : AppCompatActivity() {
}
private fun openReadme() {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(
"https://github.com/markusfisch/BinaryEye/blob/master/README.md")))
startActivity(
Intent(
Intent.ACTION_VIEW, Uri.parse(
"https://github.com/markusfisch/BinaryEye/blob/master/README.md"
)
)
)
}
private fun handleSendText(intent: Intent) {
if (!Intent.ACTION_SEND.equals(intent.getAction()) ||
!"text/plain".equals(intent.getType())) {
!"text/plain".equals(intent.getType())) {
return
}
@@ -182,9 +188,11 @@ class CameraActivity : AppCompatActivity() {
val permission = android.Manifest.permission.CAMERA
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(permission),
REQUEST_CAMERA)
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, arrayOf(permission),
REQUEST_CAMERA
)
return false
}
@@ -194,7 +202,8 @@ class CameraActivity : AppCompatActivity() {
private fun initCameraView() {
cameraView.setOnCameraListener(object : CameraView.OnCameraListener {
override fun onConfigureParameters(
parameters: Camera.Parameters) {
parameters: Camera.Parameters
) {
if (parameters.isZoomSupported()) {
val max = parameters.getMaxZoom()
if (zoomBar.max != max) {
@@ -208,7 +217,7 @@ class CameraActivity : AppCompatActivity() {
}
for (mode in parameters.getSupportedSceneModes()) {
if (mode.equals(Camera.Parameters.SCENE_MODE_BARCODE)) {
parameters.setSceneMode(mode)
parameters.sceneMode = mode
break
}
}
@@ -216,8 +225,11 @@ class CameraActivity : AppCompatActivity() {
}
override fun onCameraError() {
Toast.makeText(this@CameraActivity, R.string.camera_error,
Toast.LENGTH_SHORT).show()
Toast.makeText(
this@CameraActivity,
R.string.camera_error,
Toast.LENGTH_SHORT
).show()
finish()
}
@@ -241,8 +253,11 @@ class CameraActivity : AppCompatActivity() {
cameraView.setOnTouchListener({ v: View, event: MotionEvent ->
val camera = cameraView.getCamera()
camera?.cancelAutoFocus()
cameraView.setFocusArea(cameraView.calculateFocusRect(
event.getX(), event.getY(), 100))
cameraView.setFocusArea(
cameraView.calculateFocusRect(
event.getX(), event.getY(), 100
)
)
camera?.autoFocus({ _: Boolean, _: Camera ->
cameraView.removeCallbacks(runnable)
cameraView.postDelayed(runnable, 3000)
@@ -254,9 +269,11 @@ class CameraActivity : AppCompatActivity() {
private fun initZoomBar() {
zoomBar.setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int,
fromUser: Boolean) {
SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar, progress: Int,
fromUser: Boolean
) {
setZoom(progress)
}
@@ -293,7 +310,8 @@ class CameraActivity : AppCompatActivity() {
private fun initFlashFab(fab: View) {
if (!packageManager.hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
PackageManager.FEATURE_CAMERA_FLASH
)) {
fab.visibility = View.GONE
} else {
fab.setOnClickListener { _ ->
@@ -305,10 +323,12 @@ class CameraActivity : AppCompatActivity() {
private fun toggleTorchMode() {
val camera = cameraView.getCamera()
val parameters = camera?.getParameters()
parameters?.setFlashMode(if (flash)
Camera.Parameters.FLASH_MODE_OFF
else
Camera.Parameters.FLASH_MODE_TORCH)
parameters?.setFlashMode(
if (flash)
Camera.Parameters.FLASH_MODE_OFF
else
Camera.Parameters.FLASH_MODE_TORCH
)
flash = flash xor true
parameters?.let { camera.setParameters(parameters) }
}
@@ -337,15 +357,28 @@ class CameraActivity : AppCompatActivity() {
private fun decodeFrame(): Result? {
frameData ?: return null
return zxing.decodeBitmap(yuvToGray.convert(
frameData!!, frameWidth, frameHeight, frameOrientation))
return zxing.decodeBitmap(
yuvToGray.convert(
frameData!!, frameWidth, frameHeight, frameOrientation
)
)
}
private fun found(result: Result) {
cancelDecoding()
vibrator.vibrate(100)
startActivity(MainActivity.getDecodeIntent(this, result.text,
result.getBarcodeFormat()))
startActivity(
MainActivity.getDecodeIntent(
this, result.text,
result.getBarcodeFormat()
)
)
}
companion object {
private const val REQUEST_CAMERA = 1
private const val ZOOM_MAX = "zoom_max"
private const val ZOOM_LEVEL = "zoom_level"
}
}
@@ -16,26 +16,6 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
class MainActivity : AppCompatActivity() {
companion object {
private val ENCODE = "encode"
private val DECODE = "decode"
private val DECODE_FORMAT = "decode_format"
fun getEncodeIntent(context: Context, text: String? = ""): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(ENCODE, text)
return intent
}
fun getDecodeIntent(context: Context, text: String,
format: BarcodeFormat): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(DECODE, text)
intent.putExtra(DECODE_FORMAT, format)
return intent
}
}
override fun onSupportNavigateUp(): Boolean {
val fm = supportFragmentManager
if (fm != null && fm.backStackEntryCount > 0) {
@@ -58,16 +38,41 @@ class MainActivity : AppCompatActivity() {
var fragment: Fragment
if (intent?.hasExtra(ENCODE) == true) {
fragment = EncodeFragment.newInstance(
intent.getStringExtra(ENCODE))
intent.getStringExtra(ENCODE)
)
} else if (intent?.hasExtra(DECODE) == true) {
fragment = DecodeFragment.newInstance(
intent.getStringExtra(DECODE),
intent.getSerializableExtra(
DECODE_FORMAT) as BarcodeFormat)
intent.getStringExtra(DECODE),
intent.getSerializableExtra(
DECODE_FORMAT
) as BarcodeFormat
)
} else {
fragment = DecodeFragment()
}
setFragment(supportFragmentManager, fragment)
}
}
companion object {
private const val ENCODE = "encode"
private const val DECODE = "decode"
private const val DECODE_FORMAT = "decode_format"
fun getEncodeIntent(context: Context, text: String? = ""): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(ENCODE, text)
return intent
}
fun getDecodeIntent(
context: Context, text: String,
format: BarcodeFormat
): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(DECODE, text)
intent.putExtra(DECODE_FORMAT, format)
return intent
}
}
}
@@ -15,7 +15,8 @@ fun addFragment(fm: FragmentManager?, fragment: Fragment) {
}
private fun getTransaction(
fm: FragmentManager,
fragment: Fragment): FragmentTransaction {
fm: FragmentManager,
fragment: Fragment
): FragmentTransaction {
return fm.beginTransaction().replace(R.id.content_frame, fragment)
}
@@ -15,9 +15,13 @@ import android.view.Window
fun initSystemBars(activity: AppCompatActivity?) {
val view = activity?.findViewById(R.id.main_layout)
if (view != null && setSystemBarColor(activity.window,
ContextCompat.getColor(activity,
R.color.primary_dark_translucent))) {
if (view != null && setSystemBarColor(
activity.window,
ContextCompat.getColor(
activity,
R.color.primary_dark_translucent
)
)) {
view.setPadding(0, getStatusBarHeight(activity.resources), 0, 0)
}
}
@@ -50,15 +54,17 @@ fun getToolBarHeight(context: Context): Int {
return if (context.theme.resolveAttribute(
android.R.attr.actionBarSize,
tv,
true))
true
))
TypedValue.complexToDimensionPixelSize(
tv.data,
context.resources.displayMetrics)
tv.data,
context.resources.displayMetrics
)
else
0
}
fun getNavigationBarSize(res:Resources): Point {
fun getNavigationBarSize(res: Resources): Point {
val size = Point(0, 0)
if (!getIdentifierBoolean(res, "config_showNavigationBar")) {
return size
@@ -66,12 +72,14 @@ fun getNavigationBarSize(res:Resources): Point {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
val conf = res.getConfiguration()
if (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
// according to https://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali
// only a screen < 600 dp is considered to be a phone
// and can move its navigation bar to the side
conf.smallestScreenWidthDp < 600) {
size.x = getIdentifierDimen(res,
"navigation_bar_height_landscape")
// according to https://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali
// only a screen < 600 dp is considered to be a phone
// and can move its navigation bar to the side
conf.smallestScreenWidthDp < 600) {
size.x = getIdentifierDimen(
res,
"navigation_bar_height_landscape"
)
return size
}
}
@@ -79,7 +87,7 @@ fun getNavigationBarSize(res:Resources): Point {
return size
}
private fun getIdentifierBoolean(res:Resources, name:String): Boolean {
private fun getIdentifierBoolean(res: Resources, name: String): Boolean {
val id = res.getIdentifier(name, "bool", "android")
return id > 0 && res.getBoolean(id)
}
@@ -15,15 +15,54 @@ import android.widget.ImageView
import android.widget.Toast
class BarcodeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
state: Bundle?
): View? {
activity.setTitle(R.string.view_barcode)
val view = inflater.inflate(
R.layout.fragment_barcode,
container,
false
)
val args = getArguments()
args?.let {
val size = args.getInt(SIZE)
val bitmap: Bitmap?
try {
bitmap = Zxing.encodeAsBitmap(
args.getString(CONTENT),
args.getSerializable(FORMAT) as BarcodeFormat,
size,
size
)
} catch (e: Exception) {
Toast.makeText(
activity, e.message,
Toast.LENGTH_SHORT
).show()
fragmentManager.popBackStack()
return null
}
view.findViewById<ImageView>(R.id.barcode).setImageBitmap(bitmap)
}
return view
}
companion object {
private val CONTENT = "content"
private val FORMAT = "format"
private val SIZE = "size"
private const val CONTENT = "content"
private const val FORMAT = "format"
private const val SIZE = "size"
fun newInstance(
content: String,
format: BarcodeFormat,
size: Int): Fragment {
content: String,
format: BarcodeFormat,
size: Int
): Fragment {
val args = Bundle()
args.putString(CONTENT, content)
args.putSerializable(FORMAT, format)
@@ -33,37 +72,4 @@ class BarcodeFragment : Fragment() {
return fragment
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
state: Bundle?): View? {
activity.setTitle(R.string.view_barcode)
val view = inflater.inflate(
R.layout.fragment_barcode,
container,
false)
val args = getArguments()
args?.let {
val size = args.getInt(SIZE)
val bitmap: Bitmap?
try {
bitmap = Zxing.encodeAsBitmap(
args.getString(CONTENT),
args.getSerializable(FORMAT) as BarcodeFormat,
size,
size)
} catch (e: Exception) {
Toast.makeText(activity, e.message,
Toast.LENGTH_SHORT).show()
fragmentManager.popBackStack()
return null
}
view.findViewById<ImageView>(R.id.barcode).setImageBitmap(bitmap)
}
return view
}
}
@@ -21,20 +21,6 @@ import android.widget.EditText
import android.widget.Toast
class DecodeFragment : Fragment() {
companion object {
private val CONTENT = "content"
private val FORMAT = "format"
fun newInstance(content: String, format: BarcodeFormat): Fragment {
val args = Bundle()
args.putString(CONTENT, content)
args.putSerializable(FORMAT, format)
val fragment = DecodeFragment()
fragment.arguments = args
return fragment
}
}
private lateinit var contentView: EditText
private lateinit var format: BarcodeFormat
@@ -44,19 +30,20 @@ class DecodeFragment : Fragment() {
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
state: Bundle?): View {
inflater: LayoutInflater,
container: ViewGroup?,
state: Bundle?
): View {
activity.setTitle(R.string.content)
val view = inflater.inflate(
R.layout.fragment_decode,
container,
false)
R.layout.fragment_decode,
container,
false
)
val content = arguments?.getString(CONTENT) ?: ""
format = arguments?.getSerializable(FORMAT) as BarcodeFormat? ?:
BarcodeFormat.QR_CODE
format = arguments?.getSerializable(FORMAT) as BarcodeFormat? ?: BarcodeFormat.QR_CODE
contentView = view.findViewById<EditText>(R.id.content)
contentView.setText(content)
@@ -83,8 +70,10 @@ class DecodeFragment : Fragment() {
true
}
R.id.create -> {
addFragment(fragmentManager,
EncodeFragment.newInstance(getContent(), format))
addFragment(
fragmentManager,
EncodeFragment.newInstance(getContent(), format)
)
true
}
else -> super.onOptionsItemSelected(item)
@@ -99,11 +88,14 @@ class DecodeFragment : Fragment() {
activity ?: return
val cm = activity.getSystemService(
Context.CLIPBOARD_SERVICE) as ClipboardManager
Context.CLIPBOARD_SERVICE
) as ClipboardManager
cm.setText(text)
Toast.makeText(activity,
R.string.put_into_clipboard,
Toast.LENGTH_SHORT).show()
Toast.makeText(
activity,
R.string.put_into_clipboard,
Toast.LENGTH_SHORT
).show()
}
private fun openUrl(url: String) {
@@ -114,9 +106,11 @@ class DecodeFragment : Fragment() {
if (intent.resolveActivity(activity.getPackageManager()) != null) {
startActivity(intent)
} else {
Toast.makeText(activity,
R.string.cannot_resolve_action,
Toast.LENGTH_SHORT).show()
Toast.makeText(
activity,
R.string.cannot_resolve_action,
Toast.LENGTH_SHORT
).show()
}
}
@@ -126,4 +120,18 @@ class DecodeFragment : Fragment() {
intent.setType("text/plain")
startActivity(intent)
}
companion object {
private const val CONTENT = "content"
private const val FORMAT = "format"
fun newInstance(content: String, format: BarcodeFormat): Fragment {
val args = Bundle()
args.putString(CONTENT, content)
args.putSerializable(FORMAT, format)
val fragment = DecodeFragment()
fragment.arguments = args
return fragment
}
}
}
@@ -20,55 +20,43 @@ import android.widget.TextView
import android.widget.Toast
class EncodeFragment : Fragment() {
companion object {
private val CONTENT = "content"
private val FORMAT = "format"
fun newInstance(
content: String,
format: BarcodeFormat = BarcodeFormat.AZTEC): Fragment {
val args = Bundle()
args.putString(CONTENT, content)
args.putSerializable(FORMAT, format)
val fragment = EncodeFragment()
fragment.setArguments(args)
return fragment
}
}
private lateinit var formatView: Spinner
private lateinit var sizeView: TextView
private lateinit var sizeBarView: SeekBar
private val writers = arrayListOf(
BarcodeFormat.AZTEC,
BarcodeFormat.CODABAR,
BarcodeFormat.CODE_39,
BarcodeFormat.CODE_128,
BarcodeFormat.DATA_MATRIX,
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13,
BarcodeFormat.ITF,
BarcodeFormat.PDF_417,
BarcodeFormat.QR_CODE,
BarcodeFormat.UPC_A
BarcodeFormat.AZTEC,
BarcodeFormat.CODABAR,
BarcodeFormat.CODE_39,
BarcodeFormat.CODE_128,
BarcodeFormat.DATA_MATRIX,
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13,
BarcodeFormat.ITF,
BarcodeFormat.PDF_417,
BarcodeFormat.QR_CODE,
BarcodeFormat.UPC_A
)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
state: Bundle?): View {
inflater: LayoutInflater,
container: ViewGroup?,
state: Bundle?
): View {
activity.setTitle(R.string.compose_barcode)
val view = inflater.inflate(
R.layout.fragment_encode,
container,
false)
R.layout.fragment_encode,
container,
false
)
formatView = view.findViewById<Spinner>(R.id.format)
formatView.setAdapter(ArrayAdapter<String>(
formatView.setAdapter(
ArrayAdapter<String>(
activity,
android.R.layout.simple_list_item_1,
writers.map { it -> it.name }))
writers.map { it -> it.name })
)
sizeView = view.findViewById<TextView>(R.id.size_display)
sizeBarView = view.findViewById<SeekBar>(R.id.size_bar)
@@ -79,8 +67,11 @@ class EncodeFragment : Fragment() {
val args = getArguments()
args?.let {
contentView.setText(args.getString(CONTENT))
formatView.setSelection(writers.indexOf(
args.getSerializable(FORMAT) as BarcodeFormat?))
formatView.setSelection(
writers.indexOf(
args.getSerializable(FORMAT) as BarcodeFormat?
)
)
}
view.findViewById<View>(R.id.encode).setOnClickListener { v ->
@@ -88,14 +79,19 @@ class EncodeFragment : Fragment() {
var size = getSize(sizeBarView.getProgress())
val content = contentView.getText().toString()
if (content.isEmpty()) {
Toast.makeText(v.context, R.string.error_no_content,
Toast.LENGTH_SHORT).show()
Toast.makeText(
v.context, R.string.error_no_content,
Toast.LENGTH_SHORT
).show()
} else {
hideSoftKeyboard(contentView)
addFragment(fragmentManager, BarcodeFragment.newInstance(
addFragment(
fragmentManager, BarcodeFragment.newInstance(
content,
format,
size))
size
)
)
}
}
@@ -105,18 +101,19 @@ class EncodeFragment : Fragment() {
private fun initSizeBar() {
updateSize(sizeBarView.getProgress())
sizeBarView.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar,
progressValue: Int,
fromUser: Boolean) {
updateSize(progressValue)
}
fromUser: Boolean
) {
updateSize(progressValue)
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
}
private fun updateSize(power: Int) {
@@ -128,9 +125,27 @@ class EncodeFragment : Fragment() {
private fun hideSoftKeyboard(view: View) {
val im = activity?.getSystemService(
Context.INPUT_METHOD_SERVICE) as InputMethodManager?
Context.INPUT_METHOD_SERVICE
) as InputMethodManager?
im?.let {
im.hideSoftInputFromWindow(view.getWindowToken(), 0)
}
}
companion object {
private const val CONTENT = "content"
private const val FORMAT = "format"
fun newInstance(
content: String,
format: BarcodeFormat = BarcodeFormat.AZTEC
): Fragment {
val args = Bundle()
args.putString(CONTENT, content)
args.putSerializable(FORMAT, format)
val fragment = EncodeFragment()
fragment.setArguments(args)
return fragment
}
}
}
@@ -42,17 +42,22 @@ class YuvToGray(context: Context) {
}
fun convert(
data: ByteArray,
width: Int,
height: Int,
orientation: Int): Bitmap {
data: ByteArray,
width: Int,
height: Int,
orientation: Int
): Bitmap {
if (dest == null) {
yuvType = Type.createXY(rs, Element.U8(rs), width, height * 3 / 2)
yuvAlloc = Allocation.createTyped(rs, yuvType,
Allocation.USAGE_SCRIPT)
yuvAlloc = Allocation.createTyped(
rs, yuvType,
Allocation.USAGE_SCRIPT
)
rgbaType = Type.createXY(rs, Element.RGBA_8888(rs), width, height)
rgbaAlloc = Allocation.createTyped(rs, rgbaType,
Allocation.USAGE_SCRIPT)
rgbaAlloc = Allocation.createTyped(
rs, rgbaType,
Allocation.USAGE_SCRIPT
)
var w = width
var h = height
@@ -66,10 +71,11 @@ class YuvToGray(context: Context) {
dest = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
destAlloc = Allocation.createFromBitmap(
rs,
dest,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT)
rs,
dest,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT
)
}
yuvAlloc?.copyFrom(data)
@@ -18,60 +18,35 @@ import java.util.EnumMap
import java.util.EnumSet
class Zxing {
companion object {
private val black = 0xff000000.toInt()
private val white = 0xffffffff.toInt()
fun encodeAsBitmap(
text: String,
format: BarcodeFormat,
width: Int,
height: Int): Bitmap? {
val result = MultiFormatWriter().encode(text, format,
width, height, null)
val w = result.getWidth()
val h = result.getHeight()
val pixels = IntArray(w * h)
var offset = 0
for (y in 0..h - 1) {
for (x in 0..w - 1) {
pixels[offset + x] = if (result.get(x, y))
black
else
white
}
offset += w
}
val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
bitmap.setPixels(pixels, 0, width, 0, 0, w, h)
return bitmap
}
}
private val multiFormatReader: MultiFormatReader = MultiFormatReader()
init {
val decodeFormats = EnumSet.noneOf<BarcodeFormat>(
BarcodeFormat::class.java)
decodeFormats.addAll(EnumSet.copyOf(Arrays.asList(
BarcodeFormat.AZTEC,
BarcodeFormat.CODABAR,
BarcodeFormat.CODE_39,
BarcodeFormat.CODE_93,
BarcodeFormat.CODE_128,
BarcodeFormat.DATA_MATRIX,
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13,
BarcodeFormat.ITF,
BarcodeFormat.MAXICODE,
BarcodeFormat.PDF_417,
BarcodeFormat.QR_CODE,
BarcodeFormat.RSS_14,
BarcodeFormat.RSS_EXPANDED,
BarcodeFormat.UPC_A,
BarcodeFormat.UPC_E,
BarcodeFormat.UPC_EAN_EXTENSION
)))
BarcodeFormat::class.java
)
decodeFormats.addAll(
EnumSet.copyOf(
Arrays.asList(
BarcodeFormat.AZTEC,
BarcodeFormat.CODABAR,
BarcodeFormat.CODE_39,
BarcodeFormat.CODE_93,
BarcodeFormat.CODE_128,
BarcodeFormat.DATA_MATRIX,
BarcodeFormat.EAN_8,
BarcodeFormat.EAN_13,
BarcodeFormat.ITF,
BarcodeFormat.MAXICODE,
BarcodeFormat.PDF_417,
BarcodeFormat.QR_CODE,
BarcodeFormat.RSS_14,
BarcodeFormat.RSS_EXPANDED,
BarcodeFormat.UPC_A,
BarcodeFormat.UPC_E,
BarcodeFormat.UPC_EAN_EXTENSION
)
)
)
val hints = EnumMap<DecodeHintType, Any>(DecodeHintType::class.java)
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats)
@@ -97,4 +72,37 @@ class Zxing {
multiFormatReader.reset()
}
}
companion object {
private const val BLACK = 0xff000000.toInt()
private const val WHITE = 0xffffffff.toInt()
fun encodeAsBitmap(
text: String,
format: BarcodeFormat,
width: Int,
height: Int
): Bitmap? {
val result = MultiFormatWriter().encode(
text, format,
width, height, null
)
val w = result.getWidth()
val h = result.getHeight()
val pixels = IntArray(w * h)
var offset = 0
for (y in 0..h - 1) {
for (x in 0..w - 1) {
pixels[offset + x] = if (result.get(x, y))
BLACK
else
WHITE
}
offset += w
}
val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
bitmap.setPixels(pixels, 0, width, 0, 0, w, h)
return bitmap
}
}
}