Skip to content

Commit

Permalink
1.0.2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzue committed May 23, 2023
1 parent 1f7cf26 commit 8b055d5
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions CameraXQRDecoder/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdk 21
targetSdk 30
versionCode 2
versionName "1.0.1"
versionName "1.0.2"
consumerProguardFiles "consumer-rules.pro"
}

Expand All @@ -20,8 +20,8 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

Expand All @@ -35,5 +35,5 @@ dependencies {
api "androidx.camera:camera-lifecycle:${camerax_version}"
api "androidx.camera:camera-view:1.0.0-alpha25"
api "androidx.camera:camera-extensions:1.0.0-alpha25"
api 'com.google.zxing:core:3.3.2'
api 'com.google.zxing:core:3.5.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.kongzue.cameraxqrdecoder.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.kongzue.cameraxqrdecoder.interfaces.OnWorkFinish;

import java.util.Map;

public class BitmapQRDecoder extends AsyncTask<String, Integer, String> {

public static boolean DEBUGMODE = true;

Bitmap decodeBitmap;
OnWorkFinish<String> callback;
Map hints;

public BitmapQRDecoder(Bitmap decodeBitmap, OnWorkFinish<String> callback) {
this.decodeBitmap = decodeBitmap;
this.callback = callback;
}

public BitmapQRDecoder(String filePath, OnWorkFinish<String> callback) {
this.decodeBitmap = readBitmapFromFile(filePath);
this.callback = callback;
}

@Override
protected String doInBackground(String... anything) {
return syncDecodeQRCode(decodeBitmap);
}

//二维码识别
public String syncDecodeQRCode(Bitmap bitmap) {
try {
Result result = new MultiFormatReader().decode(bitmap2BinaryBitmap(bitmap),hints);
String r = result.getText();
log("#syncDecodeQRCode.ok: " + r);
callback.finish(r);
return r;
} catch (Exception ex) {
log("#syncDecodeQRCode.error: " + ex);
if (DEBUGMODE){
ex.printStackTrace();
}
callback.failed(ex);
return "";
}
}

private void log(String s) {
if (DEBUGMODE) Log.d(">>>", s);
}

//解码位图并压缩
public static Bitmap readBitmapFromFile(String filePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, 100, 100);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}

private BinaryBitmap bitmap2BinaryBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
//获取像素
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
return new BinaryBitmap(new HybridBinarizer(source));
}

public Bitmap getDecodeBitmap() {
return decodeBitmap;
}

public BitmapQRDecoder setDecodeBitmap(Bitmap decodeBitmap) {
this.decodeBitmap = decodeBitmap;
return this;
}

public OnWorkFinish<String> getCallback() {
return callback;
}

public BitmapQRDecoder setCallback(OnWorkFinish<String> callback) {
this.callback = callback;
return this;
}

public Map getHints() {
return hints;
}

public BitmapQRDecoder setHints(Map hints) {
this.hints = hints;
return this;
}

public void start() {
execute();
}
}
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

此库基于[hotstu](https://github.com/hotstu)/**[QRCodeCameraX](https://github.com/hotstu/QRCodeCameraX)** ,由 Kotlin 改为 Java ,并加以封装。

额外新增工具 BitmapQRDecoder 可直接从 bitmap 或文件读取位图并解析其中的二维码,方便实现「从相册打开二维码」的功能。

当前版本仅保留 zxing 实现,不包含 firebase。

### 引入方法
Expand All @@ -30,13 +32,13 @@ allprojects {

```
dependencies {
implementation 'com.github.kongzue:CameraXQRDecoder:1.0.1'
implementation 'com.github.kongzue:CameraXQRDecoder:1.0.2'
}
```

3. Sync 即可

### 使用方法
### 扫码使用方法

进入 Activity 的 xml 界面编辑添加布局:

Expand Down Expand Up @@ -75,6 +77,24 @@ qrCodeView.setKeepScan(true).start(new OnWorkFinish<String>() {
qrCodeView.setFlashOpen(!qrCodeView.isFlashOpen());
```

### 从 Bitmap 位图解码

```java
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.test_decode);
new BitmapQRDecoder(bitmap, new OnWorkFinish<String>() {
@Override
public void finish(String s) {
tip(s);
}
@Override
public void failed(Object e) {
tip(e.toString());
}
}).start();
```

另外,BitmapQRDecoder 参数也可以是 filePath 图片文件路径,请确保自己的 app 已经获取文件读写相关权限。

### 开源协议

DialogX 遵循 Apache License 2.0 开源协议。
Expand Down
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

dependencies {

//kongzue 自家组件库,非必须
implementation 'com.github.kongzue:BaseFramework:6.7.9.6'
implementation 'com.github.kongzue:BaseFramework:6.8.9.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

//自家对话框库,非必须
def dialogx_version = "0.0.43.beta6"
def dialogx_version = "0.0.48"
implementation "com.github.kongzue.DialogX:DialogX:${dialogx_version}"

implementation 'androidx.appcompat:appcompat:1.3.1'
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
android:supportsRtl="true"
android:theme="@style/Theme.CameraXQRDecoderDemo">
<activity
android:name=".MainActivity"
android:exported="true">
android:name=".DecodeBitmapActivity"
android:exported="true" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.kongzue.cameraxqrdecoderdemo;

import static com.kongzue.dialogx.dialogs.PopTip.tip;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

import com.kongzue.baseframework.BaseActivity;
import com.kongzue.baseframework.interfaces.DarkNavigationBarTheme;
import com.kongzue.baseframework.interfaces.DarkStatusBarTheme;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.cameraxqrdecoder.interfaces.OnWorkFinish;
import com.kongzue.cameraxqrdecoder.util.BitmapQRDecoder;

@Layout(R.layout.activity_decode_bitmap)
@DarkStatusBarTheme(value = true)
@DarkNavigationBarTheme(value = true)
public class DecodeBitmapActivity extends BaseActivity {

@Override
public void initViews() {

}

@Override
public void initDatas(JumpParameter parameter) {

}

@Override
public void setEvents() {

}

public void TestClick(View view) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.test_decode);
new BitmapQRDecoder(bitmap, new OnWorkFinish<String>() {
@Override
public void finish(String s) {
tip(s);
}

@Override
public void failed(Object e) {
tip(e.toString());
}
}).start();
}
}
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_decode_bitmap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".DecodeBitmapActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="TestClick"
android:text="Test"/>

</RelativeLayout>
Binary file added app/src/main/res/mipmap-xxxhdpi/test_decode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.android.tools.build:gradle:7.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Aug 28 09:27:03 CST 2021
#Tue May 23 16:15:15 CST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 8b055d5

Please sign in to comment.