Skip to content

Commit 2043e0f

Browse files
committed
Use separate crop handle config for image scanning
These are two different use cases.
1 parent b93eb59 commit 2043e0f

File tree

5 files changed

+104
-51
lines changed

5 files changed

+104
-51
lines changed

app/src/main/kotlin/de/markusfisch/android/binaryeye/activity/CameraActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class CameraActivity : AppCompatActivity() {
149149
super.onDestroy()
150150
fallbackBuffer = null
151151
saveZoom()
152-
detectorView.saveCropHandlePos()
152+
detectorView.storeCropHandlePos(CAMERA_CROP_HANDLE)
153153
releaseToneGenerators()
154154
}
155155

@@ -622,7 +622,7 @@ class CameraActivity : AppCompatActivity() {
622622
updateFrameRoiAndMappingMatrix()
623623
}
624624
detectorView.setPaddingFromWindowInsets()
625-
detectorView.restoreCropHandlePos()
625+
detectorView.restoreCropHandlePos(CAMERA_CROP_HANDLE)
626626
}
627627

628628
private fun updateFrameRoiAndMappingMatrix() {
@@ -722,6 +722,8 @@ class CameraActivity : AppCompatActivity() {
722722
}
723723

724724
companion object {
725+
const val CAMERA_CROP_HANDLE = "camera_crop_handle"
726+
725727
private const val PICK_FILE_RESULT_CODE = 1
726728
private const val ZOOM_MAX = "zoom_max"
727729
private const val ZOOM_LEVEL = "zoom_level"

app/src/main/kotlin/de/markusfisch/android/binaryeye/activity/PickActivity.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import de.markusfisch.android.binaryeye.graphics.fixTransparency
2020
import de.markusfisch.android.binaryeye.graphics.loadImageUri
2121
import de.markusfisch.android.binaryeye.graphics.mapPosition
2222
import de.markusfisch.android.binaryeye.media.releaseToneGenerators
23+
import de.markusfisch.android.binaryeye.preference.Preferences
2324
import de.markusfisch.android.binaryeye.view.colorSystemAndToolBars
2425
import de.markusfisch.android.binaryeye.view.initBars
2526
import de.markusfisch.android.binaryeye.view.scanFeedback
@@ -99,7 +100,7 @@ class PickActivity : AppCompatActivity() {
99100
scanWithinBounds(bitmap)
100101
}
101102
detectorView.setPaddingFromWindowInsets()
102-
detectorView.restoreCropHandlePos()
103+
detectorView.restoreCropHandlePos(PICKER_CROP_HANDLE)
103104

104105
findViewById(R.id.scan).setOnClickListener {
105106
showResult()
@@ -195,7 +196,7 @@ class PickActivity : AppCompatActivity() {
195196

196197
override fun onDestroy() {
197198
super.onDestroy()
198-
detectorView.saveCropHandlePos()
199+
detectorView.storeCropHandlePos(PICKER_CROP_HANDLE)
199200
parentJob.cancel()
200201
releaseToneGenerators()
201202
}
@@ -259,6 +260,10 @@ class PickActivity : AppCompatActivity() {
259260
applicationContext.toast(R.string.no_barcode_found)
260261
}
261262
}
263+
264+
companion object {
265+
private const val PICKER_CROP_HANDLE = "picker_crop_handle"
266+
}
262267
}
263268

264269
private fun getNormalizedRoi(imageRect: RectF, roi: Rect): RectF {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.markusfisch.android.binaryeye.preference
2+
3+
import android.content.SharedPreferences
4+
import org.json.JSONObject
5+
6+
data class CropHandle(
7+
val active: Boolean = true,
8+
val x: Int = -2, // -2 means set default roi.
9+
val y: Int = -2,
10+
val orientation: Int = 0
11+
)
12+
13+
fun SharedPreferences.storeCropHandle(
14+
name: String,
15+
cropHandle: CropHandle
16+
) {
17+
edit().putString(
18+
name,
19+
JSONObject().apply {
20+
put("active", cropHandle.active)
21+
put("x", cropHandle.x)
22+
put("y", cropHandle.y)
23+
put("orientation", cropHandle.orientation)
24+
}.toString()
25+
).apply()
26+
}
27+
28+
fun SharedPreferences.restoreCropHandle(
29+
name: String,
30+
default: CropHandle = CropHandle()
31+
): CropHandle {
32+
val s = getString(name, null) ?: return default
33+
return try {
34+
val json = JSONObject(s)
35+
CropHandle(
36+
json.getBoolean("active"),
37+
json.getInt("x"),
38+
json.getInt("y"),
39+
json.getInt("orientation")
40+
)
41+
} catch (e: Exception) {
42+
return default
43+
}
44+
}

app/src/main/kotlin/de/markusfisch/android/binaryeye/preference/Preferences.kt

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.media.ToneGenerator
77
import android.os.Build
88
import android.preference.PreferenceManager
99
import android.support.annotation.RequiresApi
10+
import de.markusfisch.android.binaryeye.activity.CameraActivity
1011
import de.markusfisch.android.zxingcpp.ZxingCpp.BarcodeFormat
1112

1213
class Preferences {
@@ -38,26 +39,6 @@ class Preferences {
3839
apply(BARCODE_FORMATS, value)
3940
field = value
4041
}
41-
var cropHandleX = -2 // -2 means set default roi.
42-
set(value) {
43-
apply(CROP_HANDLE_X, value)
44-
field = value
45-
}
46-
var cropHandleY = -2
47-
set(value) {
48-
apply(CROP_HANDLE_Y, value)
49-
field = value
50-
}
51-
var cropHandleOrientation = 0
52-
set(value) {
53-
apply(CROP_HANDLE_ORIENTATION, value)
54-
field = value
55-
}
56-
var showCropHandle = true
57-
set(value) {
58-
apply(SHOW_CROP_HANDLE, value)
59-
field = value
60-
}
6142
var zoomBySwiping = true
6243
set(value) {
6344
apply(ZOOM_BY_SWIPING, value)
@@ -232,16 +213,8 @@ class Preferences {
232213
}
233214
}
234215

235-
cropHandleX = preferences.getInt(CROP_HANDLE_X, cropHandleX)
236-
cropHandleY = preferences.getInt(CROP_HANDLE_Y, cropHandleY)
237-
cropHandleOrientation = preferences.getInt(
238-
CROP_HANDLE_ORIENTATION,
239-
cropHandleOrientation
240-
)
241-
showCropHandle = preferences.getBoolean(
242-
SHOW_CROP_HANDLE,
243-
showCropHandle
244-
)
216+
migrateCropHandle()
217+
245218
zoomBySwiping = preferences.getBoolean(ZOOM_BY_SWIPING, zoomBySwiping)
246219
autoRotate = preferences.getBoolean(AUTO_ROTATE, autoRotate)
247220
tryHarder = preferences.getBoolean(TRY_HARDER, tryHarder)
@@ -350,6 +323,32 @@ class Preferences {
350323
}
351324
}
352325

326+
private fun migrateCropHandle() {
327+
val showCropHandleName = "show_crop_handle"
328+
if (!preferences.getBoolean(showCropHandleName, false)) {
329+
return;
330+
}
331+
val def = CropHandle()
332+
storeCropHandle(
333+
CameraActivity.CAMERA_CROP_HANDLE,
334+
CropHandle(
335+
true,
336+
preferences.getInt("crop_handle_x", def.x),
337+
preferences.getInt("crop_handle_y", def.y),
338+
preferences.getInt("crop_handle_orientation", def.orientation)
339+
)
340+
)
341+
preferences.edit().putBoolean(showCropHandleName, false).apply()
342+
}
343+
344+
fun restoreCropHandle(
345+
name: String
346+
) = preferences.restoreCropHandle(name)
347+
348+
fun storeCropHandle(name: String, cropHandle: CropHandle) {
349+
preferences.storeCropHandle(name, cropHandle)
350+
}
351+
353352
fun beepTone() = when (beepToneName) {
354353
"tone_cdma_confirm" -> ToneGenerator.TONE_CDMA_CONFIRM
355354
"tone_sup_radio_ack" -> ToneGenerator.TONE_SUP_RADIO_ACK
@@ -368,9 +367,6 @@ class Preferences {
368367
private fun put(label: String, value: Boolean) =
369368
preferences.edit().putBoolean(label, value)
370369

371-
private fun put(label: String, value: String) =
372-
preferences.edit().putString(label, value)
373-
374370
private fun apply(label: String, value: Boolean) {
375371
put(label, value).apply()
376372
}
@@ -399,10 +395,6 @@ class Preferences {
399395
}
400396

401397
private const val BARCODE_FORMATS = "formats"
402-
private const val CROP_HANDLE_X = "crop_handle_x"
403-
private const val CROP_HANDLE_Y = "crop_handle_y"
404-
private const val CROP_HANDLE_ORIENTATION = "crop_handle_orientation"
405-
private const val SHOW_CROP_HANDLE = "show_crop_handle"
406398
private const val ZOOM_BY_SWIPING = "zoom_by_swiping"
407399
private const val AUTO_ROTATE = "auto_rotate"
408400
private const val TRY_HARDER = "try_harder"

app/src/main/kotlin/de/markusfisch/android/binaryeye/widget/DetectorView.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import de.markusfisch.android.binaryeye.R
1919
import de.markusfisch.android.binaryeye.app.prefs
2020
import de.markusfisch.android.binaryeye.graphics.getBitmapFromDrawable
2121
import de.markusfisch.android.binaryeye.graphics.getDashedBorderPaint
22+
import de.markusfisch.android.binaryeye.preference.CropHandle
2223
import kotlin.math.abs
2324
import kotlin.math.max
2425
import kotlin.math.min
@@ -58,6 +59,7 @@ class DetectorView : View {
5859
private val padding: Int
5960

6061
private var coordinatesLast = 0
62+
private var showCropHandle = true
6163
private var handleGrabbed = false
6264
private var handleActive = false
6365
private var minY = 0
@@ -83,11 +85,17 @@ class DetectorView : View {
8385
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
8486
super(context, attrs, defStyleAttr)
8587

86-
fun saveCropHandlePos() {
88+
fun storeCropHandlePos(name: String) {
8789
val pos = getCropHandlePos()
88-
prefs.cropHandleX = pos.x
89-
prefs.cropHandleY = pos.y
90-
prefs.cropHandleOrientation = currentOrientation
90+
prefs.storeCropHandle(
91+
name,
92+
CropHandle(
93+
showCropHandle,
94+
pos.x,
95+
pos.y,
96+
currentOrientation
97+
)
98+
)
9199
}
92100

93101
private fun getCropHandlePos() = if (handleActive) {
@@ -96,11 +104,13 @@ class DetectorView : View {
96104
Point(-1, -1)
97105
}
98106

99-
fun restoreCropHandlePos() {
107+
fun restoreCropHandlePos(name: String) {
108+
val cropHandle = prefs.restoreCropHandle(name)
109+
showCropHandle = cropHandle.active
100110
setCropHandlePos(
101-
prefs.cropHandleX,
102-
prefs.cropHandleY,
103-
prefs.cropHandleOrientation
111+
cropHandle.x,
112+
cropHandle.y,
113+
cropHandle.orientation
104114
)
105115
}
106116

@@ -155,7 +165,7 @@ class DetectorView : View {
155165
val y = event.y.roundToInt()
156166
return when (event.actionMasked) {
157167
MotionEvent.ACTION_DOWN -> {
158-
if (prefs.showCropHandle) {
168+
if (showCropHandle) {
159169
touchDown.set(x, y)
160170
handleGrabbed = abs(x - handlePos.x) < handleXRadius &&
161171
abs(y - handlePos.y) < handleYRadius
@@ -273,7 +283,7 @@ class DetectorView : View {
273283
canvas.drawClip()
274284
}
275285
canvas.drawDots()
276-
if (prefs.showCropHandle) {
286+
if (showCropHandle) {
277287
canvas.drawHandle()
278288
}
279289
}

0 commit comments

Comments
 (0)