-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
241 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
CameraXQRDecoder/src/main/java/com/kongzue/cameraxqrdecoder/util/BitmapQRDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/kongzue/cameraxqrdecoderdemo/DecodeBitmapActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |