Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.bingoogolapple.qrcode.core;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.text.TextUtils;
Expand All @@ -14,12 +15,14 @@ class ProcessDataTask extends AsyncTask<Void, Void, ScanResult> {
private String mPicturePath;
private Bitmap mBitmap;
private WeakReference<QRCodeView> mQRCodeViewRef;
protected WeakReference<ScanBoxView> mScanBoxViewRef;
private static long sLastStartTime = 0;

ProcessDataTask(Camera camera, byte[] data, QRCodeView qrCodeView, boolean isPortrait) {
ProcessDataTask(Camera camera, byte[] data, QRCodeView qrCodeView, ScanBoxView scanBoxView, boolean isPortrait) {
mCamera = camera;
mData = data;
mQRCodeViewRef = new WeakReference<>(qrCodeView);
mScanBoxViewRef = new WeakReference<>(scanBoxView);
mIsPortrait = isPortrait;
}

Expand Down Expand Up @@ -48,11 +51,12 @@ void cancelTask() {
protected void onCancelled() {
super.onCancelled();
mQRCodeViewRef.clear();
mScanBoxViewRef.clear();
mBitmap = null;
mData = null;
}

private ScanResult processData(QRCodeView qrCodeView) {
private ScanResult processData(QRCodeView qrCodeView, ScanBoxView scanBoxView) {
if (mData == null) {
return null;
}
Expand All @@ -67,17 +71,33 @@ private ScanResult processData(QRCodeView qrCodeView) {
height = size.height;

if (mIsPortrait) {
data = new byte[mData.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
data[x * height + height - y - 1] = mData[x + y * width];
Rect rect = scanBoxView.getScanBoxAreaRect(width);

if (rect != null) {
int frameWidth = rect.right - rect.left;
int frameHeight = rect.bottom - rect.top;
data = new byte[frameWidth * frameHeight];

for (int y = 0; y < frameWidth; y++) {
for (int x = 0; x < frameHeight; x++) {
data[x * frameWidth + frameWidth - y - 1] = mData[(rect.top + x - 1) + (y + height - rect.right) * width];
}
}

width = frameWidth;
height = frameHeight;
} else {
data = new byte[mData.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
data[x * height + height - y - 1] = this.mData[x + y * width];
}
}
int tmp = width;
width = height;
height = tmp;
}
int tmp = width;
width = height;
height = tmp;
}

return qrCodeView.processData(data, width, height, false);
} catch (Exception e1) {
e1.printStackTrace();
Expand All @@ -98,6 +118,7 @@ private ScanResult processData(QRCodeView qrCodeView) {
@Override
protected ScanResult doInBackground(Void... params) {
QRCodeView qrCodeView = mQRCodeViewRef.get();
ScanBoxView scanBoxView = mScanBoxViewRef.get();
if (qrCodeView == null) {
return null;
}
Expand All @@ -115,7 +136,7 @@ protected ScanResult doInBackground(Void... params) {
}
long startTime = System.currentTimeMillis();

ScanResult scanResult = processData(qrCodeView);
ScanResult scanResult = processData(qrCodeView, scanBoxView);

if (BGAQRCodeUtil.isDebug()) {
long time = System.currentTimeMillis() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void onPreviewFrame(final byte[] data, final Camera camera) {
return;
}

mProcessDataTask = new ProcessDataTask(camera, data, this, BGAQRCodeUtil.isPortrait(getContext())).perform();
mProcessDataTask = new ProcessDataTask(camera, data, this, mScanBoxView, BGAQRCodeUtil.isPortrait(getContext())).perform();
}

private void handleAmbientBrightness(byte[] data, Camera camera) {
Expand Down