Skip to content

Commit

Permalink
提升稳定性,完善图片demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Jul 22, 2020
1 parent 5a99631 commit 06348b2
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 28 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ android {
dependencies {
implementation project(path: ':module_camera')
api "androidx.appcompat:appcompat:1.1.0"
implementation 'com.zhihu.android:matisse:0.5.3-beta3'
// implementation 'com.github.ailiwean:NBZxing:0.0.7'
// api 'com.google.zxing:core:3.3.3'
// implementation 'com.github.ailiwean:NBZxing:0.0.12'
Expand Down
75 changes: 70 additions & 5 deletions app/src/main/java/com/android/NBZxing/CusZxingView.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.android.NBZxing

import android.Manifest
import android.app.Activity
import android.content.Context
import android.os.Environment
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.ailiwean.core.view.ZxingCameraView
import com.ailiwean.core.zxing.ScanTypeConfig
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine


/**
* @Package: com.android.NBZXing
Expand All @@ -27,12 +37,42 @@ class CusZxingView @JvmOverloads constructor(context: Context, attributeSet: Att
*/
override fun provideFloorView(): View? {
val v = LayoutInflater.from(context)
.inflate(R.layout.floorview_layout, this, false)
.inflate(R.layout.tool_title, this, false)

v.findViewById<View>(R.id.vToolBar)
.setBackgroundColor(Color.parseColor("#2f000000"))

v.findViewById<TextView>(R.id.vTitle).text = "扫一扫"

v.findViewById<View>(R.id.img)
.setOnClickListener {
parseFile(Environment.getExternalStorageDirectory().absolutePath + "/scan.jpg")
v.findViewById<View>(R.id.vLeftImage)
.setOnClickListener { v: View? ->
if (context is Activity) {
(context as Activity).finish()
}
}

v.findViewById<TextView>(R.id.vRightTextView).text = "相册"
v.findViewById<TextView>(R.id.vRightTextView)
.setOnClickListener { v: View? ->
if (!checkPermissionRW()) {
requstPermissionRW()
return@setOnClickListener
}
if (context is Activity) {
Matisse.from(context as Activity)
.choose(MimeType.ofAll())
.countable(true)
.maxSelectable(9)
.gridExpectedSize(300)
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
.thumbnailScale(0.85f)
.imageEngine(GlideEngine())
.showPreview(false) // Default is `true`
.forResult(1)
}

}

return v
}

Expand All @@ -48,4 +88,29 @@ class CusZxingView @JvmOverloads constructor(context: Context, attributeSet: Att
return ScanTypeConfig.HIGH_FREQUENCY
}

fun toParse(string: String) {
parseFile(string)
}


fun checkPermissionRW(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

} else {
return true
}
}


fun requstPermissionRW() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(context as? Activity)?.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), 200)
}
}

}
25 changes: 24 additions & 1 deletion app/src/main/java/com/android/NBZxing/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.android.NBZxing;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.zhihu.matisse.Matisse;

import java.util.List;


public class MainActivity extends AppCompatActivity {
@Override
Expand All @@ -20,4 +29,18 @@ protected void onCreate(Bundle savedInstanceState) {
// .replace(R.id.parent, fragment)
// .commit();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String path = Matisse.obtainPathResult(data).get(0);
this.<CusZxingView>findViewById(R.id.zxingview).toParse(path);
} else
Toast.makeText(this, "uri为null", Toast.LENGTH_SHORT).show();
}

}

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_fanhui.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:viewportHeight="1024"
android:viewportWidth="1024" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:pathData="M352.77,533.82c-5.63,0 -11.23,-2.14 -15.51,-6.43 -8.57,-8.57 -8.57,-22.45 0,-31.02L655.72,177.94c8.57,-8.57 22.45,-8.57 31.02,0 8.57,8.57 8.57,22.45 0,31.02L368.28,527.39C364,531.68 358.4,533.82 352.77,533.82z"/>
<path android:fillColor="#ffffff" android:pathData="M670.05,851.1c-5.63,0 -11.23,-2.14 -15.51,-6.43L337.26,527.39c-8.57,-8.57 -8.57,-22.45 0,-31.02s22.45,-8.57 31.02,0l317.28,317.28c8.57,8.57 8.57,22.45 0,31.02C681.28,848.95 675.68,851.1 670.05,851.1z"/>
</vector>
8 changes: 1 addition & 7 deletions app/src/main/res/layout/floorview_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff000000"
android:gravity="center"
android:text="点击识别图片" />


</LinearLayout>
84 changes: 84 additions & 0 deletions app/src/main/res/layout/tool_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/vToolBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
tools:ignore="ResourceName">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">


<TextView
android:id="@+id/vLeftTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="12dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:textColor="@color/colorPrimary"
android:visibility="gone" />

<ImageView
android:id="@+id/vLeftImage"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="6dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:src="@drawable/ic_fanhui"
android:visibility="visible" />


<TextView
android:id="@+id/vTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:ellipsize="middle"
android:gravity="center"
android:maxWidth="280dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="17sp" />

<TextView
android:id="@+id/vRightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:text=""
android:textColor="#FFFFFF" />

<ImageView
android:id="@+id/vRightImage"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="6dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:visibility="gone" />

</RelativeLayout>

</RelativeLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<!--对应外部内存卡根目录:Environment.getExternalStorageDirectory()-->
<external-path
name="external-path"
path="" />
</paths>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.graphics.PointF
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -23,7 +22,6 @@ import com.ailiwean.core.zxing.BitmapLuminanceSource
import com.ailiwean.core.zxing.CustomMultiFormatReader
import com.ailiwean.core.zxing.ScanTypeConfig
import com.ailiwean.core.zxing.core.BinaryBitmap
import com.ailiwean.core.zxing.core.RGBLuminanceSource
import com.ailiwean.core.zxing.core.common.HybridBinarizer
import com.google.android.cameraview.AspectRatio
import com.google.android.cameraview.BaseCameraView
Expand Down Expand Up @@ -86,7 +84,7 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
* 扫码成功后的一些动作
*/
fun scanSucHelper() {
stop()
onComPause()
scan_bar.stopAnim()
VibrateHelper.playVibrate()
VibrateHelper.playBeep()
Expand Down Expand Up @@ -191,20 +189,29 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
if (!file.exists()) {
return
}
val bitmap = BitmapFactory.decodeFile(filePath)
parseBitmap(bitmap)
cameraHandler.post {
val bitmap = BitmapFactory.decodeFile(filePath)
parseBitmap(bitmap)
}
}

protected fun parseBitmap(bitmap: Bitmap) {
val source = BitmapLuminanceSource(bitmap)
val result = CustomMultiFormatReader.getInstance()
.decode(BinaryBitmap(HybridBinarizer(source)))
if (result != null) {
resultBackFile(result.text)
scanSucHelper()
} else {
resultBackFile("")
onComResume()
onComPause()
cameraHandler.post {
val source = BitmapLuminanceSource(bitmap)
val result = CustomMultiFormatReader.getInstance()
.decode(BinaryBitmap(HybridBinarizer(source)))
if (result != null) {
hand.post {
resultBackFile(result.text)
scanSucHelper()
}
} else {
hand.post {
resultBackFile("")
onComResume()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
isShoudCreateOpen = false
}

private val cameraHandler by lazy {
protected val cameraHandler by lazy {
val handlerThread: HandlerThread = HandlerThread(System.currentTimeMillis().toString())
handlerThread.start()
Handler(handlerThread.looper)
Expand Down

0 comments on commit 06348b2

Please sign in to comment.