Skip to content
Open
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
143 changes: 94 additions & 49 deletions android/src/main/java/com/eguma/barcodescanner/BarcodeScannerView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.eguma.barcodescanner;


import android.os.AsyncTask;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
Expand Down Expand Up @@ -56,61 +58,104 @@ public void stopCamera() {
mPreview.stopCamera();
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
try {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
int width = size.width;
int height = size.height;

if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}

int tmp = width;
width = height;
height = tmp;
data = rotatedData;
}

Result rawResult = null;
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
public class ProcessFrameData {
public byte[] data;
public Camera camera;

public ProcessFrameData(byte[] data, Camera camera) {
this.data = data;
this.camera = camera;
}
}

private class ProcessFrameTask extends AsyncTask<ProcessFrameData, Void, Void> {
protected Void doInBackground(ProcessFrameData... objArr) {

byte[] data = objArr[0].data;
Camera camera = objArr[0].camera;

if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

try {
rawResult = mMultiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
// continue
} catch (NullPointerException npe) {
// This is terrible
} catch (ArrayIndexOutOfBoundsException aoe) {

} finally {
mMultiFormatReader.reset();
}
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
int width = size.width;
int height = size.height;

if (DisplayUtils.getScreenOrientation(getContext()) == Configuration.ORIENTATION_PORTRAIT) {
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}

int tmp = width;
width = height;
height = tmp;
data = rotatedData;
}

Result rawResult = null;
PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);

if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
rawResult = mMultiFormatReader.decodeWithState(bitmap);
} catch (ReaderException re) {
// continue
} catch (NullPointerException npe) {
// This is terrible
} catch (ArrayIndexOutOfBoundsException aoe) {

} finally {
mMultiFormatReader.reset();
}
}

final Result finalRawResult = rawResult;

if (finalRawResult != null) {
Log.i(TAG, finalRawResult.getText());
WritableMap event = Arguments.createMap();
event.putString("data", finalRawResult.getText());
event.putString("type", finalRawResult.getBarcodeFormat().toString());
ReactContext reactContext = (ReactContext)getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
"topChange",
event);
}
} catch(Exception e) {
// TODO: Terrible hack. It is possible that this method is invoked after camera is released.
Log.e(TAG, e.toString(), e);
}

return null;
}

final Result finalRawResult = rawResult;

if (finalRawResult != null) {
Log.i(TAG, finalRawResult.getText());
WritableMap event = Arguments.createMap();
event.putString("data", finalRawResult.getText());
event.putString("type", finalRawResult.getBarcodeFormat().toString());
ReactContext reactContext = (ReactContext)getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
"topChange",
event);
protected void onProgressUpdate(Integer... progress) {

}
} catch(Exception e) {
// TODO: Terrible hack. It is possible that this method is invoked after camera is released.
Log.e(TAG, e.toString(), e);

protected void onPostExecute() {

}
}


public ProcessFrameTask CURRENT_TASK;
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// only allow one running task
if(CURRENT_TASK == null || CURRENT_TASK.getStatus() == AsyncTask.Status.FINISHED ) {
if(camera != null) {
// AsyncTask expects one parameter, so I wrap data and camera in a class
ProcessFrameData frameDataHeloer = new ProcessFrameData(data, camera);
Copy link
Copy Markdown

@grunch grunch Oct 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexSchwabauer Maybe you want to change "frameDataHeloer" for "frameDataHelper" :)

CURRENT_TASK = new ProcessFrameTask();
CURRENT_TASK.execute(frameDataHeloer);
}

}
}
}