move rotation to BarCodeScannerAsyncTask (#1667)

This commit is contained in:
SimonErm 2018-06-29 16:43:21 +02:00 committed by Sibelius Seraphini
parent 0b816adbfd
commit 236758147d
2 changed files with 20 additions and 29 deletions

View File

@ -107,26 +107,9 @@ public class RNCameraView extends CameraView implements LifecycleEventListener,
}
}
private byte[] rotateImage(byte[] imageData, int height, int width) {
byte[] rotated = new byte[imageData.length];
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
int sourceIx = x + y * height;
int destIx = x * width + width - y - 1;
if (sourceIx >= 0 && sourceIx < imageData.length && destIx >= 0 && destIx < imageData.length) {
rotated[destIx] = imageData[sourceIx];
}
}
}
return rotated;
}
@Override
public void onFramePreview(CameraView cameraView, byte[] data, int width, int height, int rotation) {
int correctRotation = RNCameraViewHelper.getCorrectCameraRotation(rotation, getFacing());
int correctWidth = width;
int correctHeight = height;
byte[] correctData = data;
boolean willCallBarCodeTask = mShouldScanBarCodes && !barCodeScannerTaskLock && cameraView instanceof BarCodeScannerAsyncTaskDelegate;
boolean willCallFaceTask = mShouldDetectFaces && !faceDetectorTaskLock && cameraView instanceof FaceDetectorAsyncTaskDelegate;
boolean willCallGoogleBarcodeTask = mShouldGoogleDetectBarcodes && !googleBarcodeDetectorTaskLock && cameraView instanceof BarcodeDetectorAsyncTaskDelegate;
@ -135,33 +118,28 @@ public class RNCameraView extends CameraView implements LifecycleEventListener,
return;
}
if (correctRotation == 90) {
correctWidth = height;
correctHeight = width;
correctData = rotateImage(data, correctHeight, correctWidth);
}
if (willCallBarCodeTask) {
barCodeScannerTaskLock = true;
BarCodeScannerAsyncTaskDelegate delegate = (BarCodeScannerAsyncTaskDelegate) cameraView;
new BarCodeScannerAsyncTask(delegate, mMultiFormatReader, correctData, correctWidth, correctHeight).execute();
new BarCodeScannerAsyncTask(delegate, mMultiFormatReader, data, width, height).execute();
}
if (willCallFaceTask) {
faceDetectorTaskLock = true;
FaceDetectorAsyncTaskDelegate delegate = (FaceDetectorAsyncTaskDelegate) cameraView;
new FaceDetectorAsyncTask(delegate, mFaceDetector, correctData, correctWidth, correctHeight, correctRotation).execute();
new FaceDetectorAsyncTask(delegate, mFaceDetector, data, width, height, correctRotation).execute();
}
if (willCallGoogleBarcodeTask) {
googleBarcodeDetectorTaskLock = true;
BarcodeDetectorAsyncTaskDelegate delegate = (BarcodeDetectorAsyncTaskDelegate) cameraView;
new BarcodeDetectorAsyncTask(delegate, mGoogleBarcodeDetector, correctData, correctWidth, correctHeight, correctRotation).execute();
new BarcodeDetectorAsyncTask(delegate, mGoogleBarcodeDetector, data, width, height, correctRotation).execute();
}
if (willCallTextTask) {
textRecognizerTaskLock = true;
TextRecognizerAsyncTaskDelegate delegate = (TextRecognizerAsyncTaskDelegate) cameraView;
new TextRecognizerAsyncTask(delegate, mTextRecognizer, correctData, correctWidth, correctHeight, correctRotation).execute();
new TextRecognizerAsyncTask(delegate, mTextRecognizer, data, width, height, correctRotation).execute();
}
}
});

View File

@ -41,14 +41,27 @@ public class BarCodeScannerAsyncTask extends android.os.AsyncTask<Void, Void, Re
BinaryBitmap bitmap = generateBitmapFromImageData(mImageData, mWidth, mHeight);
result = mMultiFormatReader.decodeWithState(bitmap);
} catch (NotFoundException e) {
// No barcode found, result is already null.
BinaryBitmap bitmap = generateBitmapFromImageData(rotateImage(mImageData,mWidth, mHeight),mHeight,mWidth);
try {
result = mMultiFormatReader.decodeWithState(bitmap);
} catch (NotFoundException e1) {
//no barcode Found
}
} catch (Throwable t) {
t.printStackTrace();
}
return result;
}
private byte[] rotateImage(byte[]imageData,int width, int height) {
byte[] rotated = new byte[imageData.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
rotated[x * height + height - y - 1] = imageData[x + y * width];
}
}
return rotated;
}
@Override
protected void onPostExecute(Result result) {
super.onPostExecute(result);