Skip to content

Commit

Permalink
mergeMerge branch 'master' of https://github.com/dupengw3/flutter_bar…
Browse files Browse the repository at this point in the history
  • Loading branch information
dupengw3 committed Jul 31, 2020
2 parents 4ae2d2d + 61b6c08 commit 3927efb
Show file tree
Hide file tree
Showing 24 changed files with 293 additions and 136 deletions.
3 changes: 2 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.CAMERA" />

<application>
<activity android:name="de.mintware.barcode_scan.BarcodeScannerActivity" />
<activity android:name="de.mintware.barcode_scan.BarcodeScannerActivity"
android:theme="@style/TranslucentTheme" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package de.mintware.barcode_scan
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.*
import android.widget.TextView
import com.google.zxing.BarcodeFormat
import com.google.zxing.Result
import me.dm7.barcodescanner.zxing.ZXingScannerView
Expand All @@ -19,8 +19,6 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
private var scannerView: ZXingScannerView? = null

companion object {
const val TOGGLE_FLASH = 200
const val CANCEL = 300
const val EXTRA_CONFIG = "config"
const val EXTRA_RESULT = "scan_result"
const val EXTRA_ERROR_CODE = "error_code"
Expand All @@ -44,16 +42,26 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
// region Activity lifecycle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_barcode_scanner)
config = Protos.Configuration.parseFrom(intent.extras!!.getByteArray(EXTRA_CONFIG))
}

private fun setupScannerView() {
if (scannerView != null) {
return
}

scannerView = ZXingAutofocusScannerView(this).apply {
val tVFlashLight = findViewById<TextView>(R.id.tv_flashlight)
findViewById<View>(R.id.back).setOnClickListener { onBackPressed() }
findViewById<View>(R.id.hand_input).setOnClickListener {
setResult(ScanResultHandler.RESULT_HAND_INPUT)
finish()
}
findViewById<View>(R.id.flashlight).setOnClickListener {
scannerView?.toggleFlash()
tVFlashLight.setText(if (scannerView?.flash == true) R.string.close_flashlight else R.string.open_flashlight)
}
scannerView = findViewById(R.id.scannerView)
scannerView?.apply {
setAutoFocus(config.android.useAutoFocus)
val restrictedFormats = mapRestrictedBarcodeTypes()
if (restrictedFormats.isNotEmpty()) {
Expand All @@ -67,37 +75,6 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
invalidateOptionsMenu()
}
}

setContentView(scannerView)
}

// region AppBar menu
override fun onCreateOptionsMenu(menu: Menu): Boolean {
var buttonText = config.stringsMap["flash_on"]
if (scannerView?.flash == true) {
buttonText = config.stringsMap["flash_off"]
}
val flashButton = menu.add(0, TOGGLE_FLASH, 0, buttonText)
flashButton.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)

val cancelButton = menu.add(0, CANCEL, 0, config.stringsMap["cancel"])
cancelButton.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)

return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == TOGGLE_FLASH) {
scannerView?.toggleFlash()
this.invalidateOptionsMenu()
return true
}
if (item.itemId == CANCEL) {
setResult(RESULT_CANCELED)
finish()
return true
}
return super.onOptionsItemSelected(item)
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ChannelHandler(private val activityHelper: ActivityHelper
.putAllStrings(mapOf(
"cancel" to "Cancel",
"flash_on" to "Flash on",
"flash_off" to "Flash off"
"flash_off" to "Flash off",
"hand_input" to "Hand input"
))
.setAndroid(Protos.AndroidConfiguration
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package de.mintware.barcode_scan

import android.animation.ValueAnimator
import android.animation.ValueAnimator.INFINITE
import android.animation.ValueAnimator.RESTART
import android.content.Context
import android.content.res.Resources
import android.graphics.*
import android.os.Build
import android.util.Log
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.LinearInterpolator
import androidx.annotation.RequiresApi
import me.dm7.barcodescanner.core.ViewFinderView
import kotlin.math.roundToInt

/**
* Created on 2020/7/21.
* 包名:de.mintware.barcode_scan
* @author 李舰舸 <[email protected]>
*/
class CustomViewFinderView(context: Context) : ViewFinderView(context) {

private var currentAnimValue: Float = 0f
private var laser : Bitmap
private val animator: ValueAnimator

init {
setLayerType(LAYER_TYPE_SOFTWARE, null);
setBorderColor(Color.parseColor("#3EBADE"))
setSquareViewFinder(true)
setBorderLineLength((Resources.getSystem().displayMetrics.density * 15).toInt())
setLaserEnabled(true)
laser = BitmapFactory.decodeResource(resources, R.drawable.laser)

animator = ValueAnimator.ofFloat(0f, 1f)
animator.addUpdateListener { animation ->
currentAnimValue = animation.animatedValue as Float
invalidate()
}
animator.duration = 2000
animator.interpolator = LinearInterpolator()
animator.repeatMode = RESTART
animator.repeatCount = INFINITE
animator.startDelay = 500
animator.start()
}

override fun drawLaser(canvas: Canvas?) {
val total = framingRect.height() + laser.height
val percent = (laser.height.toFloat() / total)
var laserheight = laser.height.toFloat()
if (currentAnimValue < percent || currentAnimValue > (1 - percent)) {
val tempValue = if (currentAnimValue > 0.5) 1 - currentAnimValue else currentAnimValue
laserheight *= tempValue / percent
}
mLaserPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)
val rect = Rect()
rect.top = (framingRect.top - laserheight + currentAnimValue * (framingRect.height() + laserheight)).toInt()
rect.left = framingRect.left
rect.right = framingRect.right
rect.bottom = (framingRect.top + currentAnimValue * (framingRect.height() + laserheight)).toInt()
canvas?.drawBitmap(laser, null, rect, mLaserPaint)
}

override fun onWindowVisibilityChanged(visibility: Int) {
if (visibility == View.GONE) {
animator.removeAllListeners()
animator.cancel()
}
super.onWindowVisibilityChanged(visibility)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import io.flutter.plugin.common.PluginRegistry.ActivityResultListener

class ScanResultHandler(private val result: MethodChannel.Result) : ActivityResultListener {

companion object {
const val RESULT_HAND_INPUT = 0x999
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {

var scanResult = ByteArray(0)
Expand All @@ -21,6 +25,12 @@ class ScanResultHandler(private val result: MethodChannel.Result) : ActivityResu
.build()
.toByteArray()
}
RESULT_HAND_INPUT -> {
scanResult = Protos.ScanResult.newBuilder()
.setType(Protos.ResultType.HandInput)
.build()
.toByteArray()
}
else -> {
val errorCode = data?.getStringExtra(BarcodeScannerActivity.EXTRA_ERROR_CODE)
scanResult = Protos.ScanResult.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package de.mintware.barcode_scan

import android.content.Context
import android.hardware.Camera
import android.util.AttributeSet
import android.util.Log
import me.dm7.barcodescanner.core.CameraWrapper
import me.dm7.barcodescanner.core.IViewFinder
import me.dm7.barcodescanner.zxing.ZXingScannerView

class ZXingAutofocusScannerView(context: Context) : ZXingScannerView(context) {
class ZXingAutofocusScannerView : ZXingScannerView {

constructor(context: Context): super(context)

constructor(context: Context, attributeSet: AttributeSet): super(context, attributeSet)

constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int): super(context, attributeSet)

private var callbackFocus = false
private var autofocusPresence = false
Expand All @@ -30,4 +38,8 @@ class ZXingAutofocusScannerView(context: Context) : ZXingScannerView(context) {
super.setAutoFocus(callbackFocus)
}
}

override fun createViewFinderView(context: Context?): IViewFinder {
return CustomViewFinderView(context!!)
}
}
Binary file added android/src/main/res/drawable-xhdpi/icon_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xhdpi/icon_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xhdpi/laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xxhdpi/icon_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xxhdpi/laser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions android/src/main/res/layout/activity_barcode_scanner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<de.mintware.barcode_scan.ZXingAutofocusScannerView
android:id="@+id/scannerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<LinearLayout
android:layout_gravity="top"
android:theme="@style/Theme.MaterialComponents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:gravity="center_vertical">
<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingVertical="12dp"
android:paddingLeft="16dp"
android:paddingRight="4dp"
android:src="@drawable/icon_back"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_search_switch"
android:textSize="16sp"
android:textColor="@android:color/white"/>
</LinearLayout>

<LinearLayout
android:layout_gravity="bottom"
android:orientation="horizontal"
android:background="#80000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/hand_input"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:src="@drawable/icon_input"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="12dp"
android:text="@string/hand_input"
android:textSize="16sp"
android:textColor="#80FFFFFF"/>

</LinearLayout>

<LinearLayout
android:id="@+id/flashlight"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:src="@drawable/icon_light"/>

<TextView
android:id="@+id/tv_flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="12dp"
android:text="@string/open_flashlight"
android:textSize="16sp"
android:textColor="#80FFFFFF"/>

</LinearLayout>


</LinearLayout>


</FrameLayout>
8 changes: 8 additions & 0 deletions android/src/main/res/values-v19/style.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
10 changes: 10 additions & 0 deletions android/src/main/res/values-v21/style.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
7 changes: 7 additions & 0 deletions android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hand_input">手动输入</string>
<string name="open_flashlight">打开手电筒</string>
<string name="close_flashlight">关闭手电筒</string>
<string name="add_search_switch">添加/查询母开关</string>
</resources>
5 changes: 5 additions & 0 deletions android/src/main/res/values/style.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar" />
</resources>
Loading

0 comments on commit 3927efb

Please sign in to comment.