Skip to content

Commit

Permalink
缩放功能可自行选择
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Jun 15, 2021
1 parent 41610ef commit 7fc816c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 102 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/com/android/NBZxing/CusScanView2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import com.google.android.cameraview.AspectRatio
* @Author: SWY
* @CreateDate: 2020/4/30 4:06 PM
*/
class CusScanView2 @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, def: Int = 0) : NBZxingView(context, attributeSet, def) {
class CusScanView2 @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
def: Int = 0
) : NBZxingView(context, attributeSet, def) {

override fun resultBack(content: Result) {
Toast.makeText(context, content.text, Toast.LENGTH_LONG).show()
Expand Down Expand Up @@ -48,4 +52,8 @@ class CusScanView2 @JvmOverloads constructor(context: Context, attributeSet: Att
else Toast.makeText(context, content.text, Toast.LENGTH_SHORT).show()
}

override fun isSupportAutoZoom(): Boolean {
return false;
}

}
19 changes: 9 additions & 10 deletions app/src/main/java/com/android/NBZxing/ScanActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.cameraview.AspectRatio
Expand All @@ -32,13 +29,15 @@ class ScanActivity : AppCompatActivity() {
// WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
// }
setContentView(R.layout.activity_main)
findViewById<CusScanView>(R.id.zxingview)
.synchLifeStart(this)
initView()
// ZxingFragment fragment = new ZxingFragment();
// getSupportFragmentManager().beginTransaction()
// .replace(R.id.parent, fragment)
// .commit();
// findViewById<CusScanView>(R.id.zxingview)
// .synchLifeStart(this)
// initView()

val fragment = ZxingFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.parent, fragment)
.commit();

}

companion object {
Expand Down
3 changes: 3 additions & 0 deletions module_camera/src/main/java/com/ailiwean/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ public static boolean hasDepencidesScale() {
//是否支持黑边二维码识别
public static boolean isSupportBlackEdge = true;

//是否支持自动缩放
public static boolean isSupportAutoZoom = true;

}
71 changes: 38 additions & 33 deletions module_camera/src/main/java/com/ailiwean/core/able/AbleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.ailiwean.core.helper.ScanHelper
import com.ailiwean.core.zxing.core.PlanarYUVLuminanceSource
import com.ailiwean.module_grayscale.Dispatch
import com.ailiwean.module_grayscale.GrayScaleDispatch
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList

/**
Expand All @@ -30,8 +29,8 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler

private val grayProcessHandler by lazy {
Handler(HandlerThread("GrayProcessThread")
.apply { start() }
.looper)
.apply { start() }
.looper)
}

init {
Expand All @@ -43,10 +42,14 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler

fun loadAbility() {
ableList.clear()
ableList.add(XQRScanCrudeAble(handlerHolder.get()))
ableList.add(XQRScanZoomAble(handlerHolder.get()))
ableList.add(XQRScanAbleRotate(handlerHolder.get()))
ableList.add(LighSolveAble(handlerHolder.get()))
ableList.apply {
add(XQRScanCrudeAble(handlerHolder.get()))
if (Config.isSupportAutoZoom)
add(XQRScanZoomAble(handlerHolder.get()))
else add(XQRScanAble(handlerHolder.get()))
add(XQRScanAbleRotate(handlerHolder.get()))
add(LighSolveAble(handlerHolder.get()))
}
// ableList.add(XQRScanAble(handlerHolder.get()))
// ableList.add(GrayscaleStrengAble(handlerHolder.get()))
}
Expand All @@ -55,7 +58,12 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler
* 相机实时数据解析
*/
public override fun cusAction(data: ByteArray, dataWidth: Int, dataHeight: Int) {
executeToParseWay2(data, dataWidth, dataHeight, ScanHelper.getScanByteRect(dataWidth, dataHeight))
executeToParseWay2(
data,
dataWidth,
dataHeight,
ScanHelper.getScanByteRect(dataWidth, dataHeight)
)
}

/***
Expand All @@ -72,11 +80,10 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler
if (able.isCycleRun(false)) {
//线程池推送任务
server.post(TypeRunnable.create(
able.isImportant(false),
//区分类型
TypeRunnable.SCALE) {
able.needParseDeploy(source, false)
})
able.isImportant(false),
//区分类型
TypeRunnable.SCALE
) { able.needParseDeploy(source, false) })
}
}
}
Expand All @@ -85,23 +92,19 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler
/***
* 解析原始数据
*/
private fun originProcess(typeRunList: ArrayList<TypeRunnable>) {
for (item in typeRunList) {
//线程池推送任务
server.post(item)
}
}

/***
* 解析原始数据
*/
private fun originProcess(source: PlanarYUVLuminanceSource, data: ByteArray, dataWidth: Int, dataHeight: Int) {
private fun originProcess(
source: PlanarYUVLuminanceSource,
data: ByteArray,
dataWidth: Int,
dataHeight: Int
) {
ableList.forEach { able ->
if (able.isCycleRun(true))
server.post(TypeRunnable.create(
able.isImportant(true),
//区分类型
TypeRunnable.NORMAL) {
able.isImportant(true),
//区分类型
TypeRunnable.NORMAL
) {
able.cusAction(data, dataWidth, dataHeight, true)
able.needParseDeploy(source, true)
})
Expand All @@ -123,7 +126,12 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler
grayscaleProcess(graySource)
}

private fun generateGlobeYUVLuminanceSource(data: ByteArray?, dataWidth: Int, dataHeight: Int, rect: Rect): PlanarYUVLuminanceSource? {
private fun generateGlobeYUVLuminanceSource(
data: ByteArray?,
dataWidth: Int,
dataHeight: Int,
rect: Rect
): PlanarYUVLuminanceSource? {
return ScanHelper.buildLuminanceSource(data, dataWidth, dataHeight, rect)
}

Expand All @@ -134,13 +142,10 @@ class AbleManager private constructor(handler: Handler) : PixsValuesAble(handler
}

override fun release() {
ableList.forEach {
it.release()
}
ableList.forEach { it.release() }
ableList.clear()
server.quit()
if (processDispatch == null)
return
if (processDispatch == null) return
grayProcessHandler.removeCallbacksAndMessages(null)
grayProcessHandler.looper.quit()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ import java.lang.ref.WeakReference
* @Author: SWY
* @CreateDate: 2020/8/23 12:38 AM
*/
abstract class FreeZxingView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, def: Int = 0) :
BaseCameraView(context, attributeSet, def), Handler.Callback, FreeInterface {
abstract class FreeZxingView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
def: Int = 0
) : BaseCameraView(context, attributeSet, def), Handler.Callback, FreeInterface {

private var ableCollect: AbleManager? = null

Expand Down Expand Up @@ -258,10 +261,10 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
removeView(it)
}
LayoutInflater.from(context).inflate(provideFloorView(), this, false)
.let {
it.id = R.id.provideViewId
addView(it)
}
.let {
it.id = R.id.provideViewId
addView(it)
}
cameraStartLaterConfig()
}

Expand All @@ -273,6 +276,7 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
private fun initScanType() {
scanTypeConfig = configScanType()
isSupportBlackEdge = isSupportBlackEdgeQrScan()
isSupportAutoZoom = isSupportAutoZoom()
}

/***
Expand Down Expand Up @@ -332,6 +336,14 @@ abstract class FreeZxingView @JvmOverloads constructor(context: Context, attribu
return true
}


/***
* 是否支持缩放
*/
open fun isSupportAutoZoom(): Boolean {
return true
}

/***
* 业务Handler
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import android.content.Context
import android.os.Handler
import android.os.HandlerThread
import android.util.AttributeSet
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import com.ailiwean.core.Config
import com.ailiwean.core.Utils
import com.ailiwean.core.helper.ZoomHelper
Expand All @@ -19,14 +20,18 @@ import com.ailiwean.core.view.LifeOwner
* @Author: SWY
* @CreateDate: 2020/4/19 12:02 AM
*/
abstract class BaseCameraView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, def: Int = 0) :
CameraView(context, attributeSet, def), LifeOwner {
abstract class BaseCameraView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
def: Int = 0
) :
CameraView(context, attributeSet, def), LifeOwner {

//保证避免多次调用start()
var isShoudCreateOpen = true
private var isShoudCreateOpen = true

//是否禁止相机
var isProscribeCamera = false
private var isProscribeCamera = false

init {
Utils.init(context)
Expand Down Expand Up @@ -76,11 +81,11 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
}

/***
* 绑定AppCompatActivity生命周期并启动相机
* 绑定生命周期并启动相机
*/
fun synchLifeStart(appCompatActivity: AppCompatActivity) {
appCompatActivity.lifecycle.addObserver(this)
appCompatActivity.lifecycle.addObserver(object : LifeOwner {
fun synchLifeStart(lifecycle: Lifecycle) {
lifecycle.addObserver(this)
lifecycle.addObserver(object : LifeOwner {
//在onCreate()中调用提升相机打开速度
override fun onCreate() {
onCameraCreate()
Expand All @@ -104,46 +109,9 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
})
}

fun synchLifeStart(fragment: Fragment) {
fragment.fragmentManager?.registerFragmentLifecycleCallbacks(object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
if (f != fragment) {
return
}
if (isShoudCreateOpen) {
onCreate()
onResume()
onCameraCreate()
} else {
onResume()
onCameraResume()
}
}

override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
if (f != fragment) {
return
}
onPause()
onCameraPause()
}
fun synchLifeStart(activity: AppCompatActivity) = this.synchLifeStart(activity.lifecycle)


override fun onFragmentStopped(fm: FragmentManager, f: Fragment) {
if (f != fragment) {
return
}
onStop()
}

override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) {
if (f != fragment) {
return
}
onDestroy()
}
}, false)
}
fun synchLifeStart(fragment: Fragment) = this.synchLifeStart(fragment.lifecycle)

private fun onCameraCreate() {
if (!isShoudCreateOpen)
Expand All @@ -156,7 +124,6 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
}
}


/***
* 外部使用该方法启动相机
*/
Expand Down Expand Up @@ -205,9 +172,9 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
val handlerThread = HandlerThread("CameraProcessThread")
handlerThread.start()
Handler(handlerThread.looper)
.apply {
provideCameraHandler(this)
}
.apply {
provideCameraHandler(this)
}
}

var cameraStartTime = 0L
Expand Down Expand Up @@ -269,6 +236,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib

}

@CallSuper
override fun onDestroy() {
cameraHandler.looper.quit()
}
Expand Down

0 comments on commit 7fc816c

Please sign in to comment.