66import com .google .zxing .PlanarYUVLuminanceSource ;
77import com .google .zxing .Result ;
88import com .google .zxing .common .HybridBinarizer ;
9+ import org .reactnative .camera .utils .ScanArea ;
10+ import android .util .Log ;
911
1012public class BarCodeScannerAsyncTask extends android .os .AsyncTask <Void , Void , Result > {
1113 private byte [] mImageData ;
1214 private int mWidth ;
1315 private int mHeight ;
1416 private BarCodeScannerAsyncTaskDelegate mDelegate ;
1517 private final MultiFormatReader mMultiFormatReader ;
16- private boolean mLimitScanArea ;
17- private float mScanAreaX ;
18- private float mScanAreaY ;
19- private float mScanAreaWidth ;
20- private float mScanAreaHeight ;
21- private int mCameraViewWidth ;
22- private int mCameraViewHeight ;
23- private float mRatio ;
18+ private ScanArea mScanArea ;
2419
25- // note(sjchmiela): From my short research it's ok to ignore rotation of the image.
2620 public BarCodeScannerAsyncTask (
2721 BarCodeScannerAsyncTaskDelegate delegate ,
2822 MultiFormatReader multiFormatReader ,
2923 byte [] imageData ,
30- int width ,
31- int height ,
32- boolean limitScanArea ,
33- float scanAreaX ,
34- float scanAreaY ,
35- float scanAreaWidth ,
36- float scanAreaHeight ,
37- int cameraViewWidth ,
38- int cameraViewHeight ,
39- float ratio
24+ ScanArea scanArea
4025 ) {
41- mImageData = imageData ;
42- mWidth = width ;
43- mHeight = height ;
26+ mScanArea = scanArea ;
27+ mWidth = mScanArea .getWidth ();
28+ mHeight = mScanArea .getHeight ();
29+ mImageData = rotateImage (imageData ,mWidth , mHeight );
4430 mDelegate = delegate ;
4531 mMultiFormatReader = multiFormatReader ;
46- mLimitScanArea = limitScanArea ;
47- mScanAreaX = scanAreaX ;
48- mScanAreaY = scanAreaY ;
49- mScanAreaWidth = scanAreaWidth ;
50- mScanAreaHeight = scanAreaHeight ;
51- mCameraViewWidth = cameraViewWidth ;
52- mCameraViewHeight = cameraViewHeight ;
53- mRatio = ratio ;
5432 }
5533
5634 @ Override
@@ -60,81 +38,38 @@ protected Result doInBackground(Void... ignored) {
6038 }
6139
6240 Result result = null ;
63- /**
64- * mCameraViewWidth and mCameraViewHeight are obtained from portait orientation
65- * mWidth and mHeight are measured with landscape orientation with Home button to the right
66- * adjustedCamViewWidth is the adjusted width fromt the Aspect ratio setting
67- */
68- int adjustedCamViewWidth = (int ) (mCameraViewHeight / mRatio );
69- float adjustedScanY = (((adjustedCamViewWidth - mCameraViewWidth ) / 2 ) + (mScanAreaY * mCameraViewWidth )) / adjustedCamViewWidth ;
70-
71- int left = (int ) (mScanAreaX * mWidth );
72- int top = (int ) (adjustedScanY * mHeight );
73- int scanWidth = (int ) (mScanAreaWidth * mWidth );
74- int scanHeight = (int ) (((mScanAreaHeight * mCameraViewWidth ) / adjustedCamViewWidth ) * mHeight );
75-
41+ boolean mLimitScanArea = mScanArea .getLimitScanArea ();
42+ int scanWidth = mScanArea .getScanWidth ();
43+ int scanHeight = mScanArea .getScanHeight ();
44+ int left = mScanArea .getLeft ();
45+ int top = mScanArea .getTop ();
46+ if (!mLimitScanArea ) {
47+ left = 0 ;
48+ top = 0 ;
49+ scanWidth = mWidth ;
50+ scanHeight = mHeight ;
51+ }
52+ PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource (
53+ mImageData ,
54+ mHeight ,
55+ mWidth ,
56+ mHeight - scanHeight - top ,
57+ left ,
58+ scanHeight ,
59+ scanWidth ,
60+ false
61+ );
62+ BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source ));
7663 try {
77- BinaryBitmap bitmap = generateBitmapFromImageData (
78- mImageData ,
79- mWidth ,
80- mHeight ,
81- false ,
82- left ,
83- top ,
84- scanWidth ,
85- scanHeight
86- );
8764 result = mMultiFormatReader .decodeWithState (bitmap );
8865 } catch (NotFoundException e ) {
89- BinaryBitmap bitmap = generateBitmapFromImageData (
90- rotateImage (mImageData ,mWidth , mHeight ),
91- mHeight ,
92- mWidth ,
93- false ,
94- mHeight - scanHeight - top ,
95- left ,
96- scanHeight ,
97- scanWidth
98- );
99- try {
100- result = mMultiFormatReader .decodeWithState (bitmap );
101- } catch (NotFoundException e1 ) {
102- BinaryBitmap invertedBitmap = generateBitmapFromImageData (
103- mImageData ,
104- mWidth ,
105- mHeight ,
106- true ,
107- mWidth - scanWidth - left ,
108- mHeight - scanHeight - top ,
109- scanWidth ,
110- scanHeight
111- );
112- try {
113- result = mMultiFormatReader .decodeWithState (invertedBitmap );
114- } catch (NotFoundException e2 ) {
115- BinaryBitmap invertedRotatedBitmap = generateBitmapFromImageData (
116- rotateImage (mImageData ,mWidth , mHeight ),
117- mHeight ,
118- mWidth ,
119- true ,
120- top ,
121- mWidth - scanWidth - left ,
122- scanHeight ,
123- scanWidth
124- );
125- try {
126- result = mMultiFormatReader .decodeWithState (invertedRotatedBitmap );
127- } catch (NotFoundException e3 ) {
128- //no barcode Found
129- }
130- }
131- }
66+ Log .w ("CAMERA_1::" , "BarCodeScannerAsyncTask doInBackground throws: " , e );
13267 } catch (Throwable t ) {
13368 t .printStackTrace ();
13469 }
135-
13670 return result ;
13771 }
72+
13873 private byte [] rotateImage (byte []imageData ,int width , int height ) {
13974 byte [] rotated = new byte [imageData .length ];
14075 for (int y = 0 ; y < height ; y ++) {
@@ -152,36 +87,4 @@ protected void onPostExecute(Result result) {
15287 }
15388 mDelegate .onBarCodeScanningTaskCompleted ();
15489 }
155-
156- private BinaryBitmap generateBitmapFromImageData (byte [] imageData , int width , int height , boolean inverse , int left , int top , int sWidth , int sHeight ) {
157- PlanarYUVLuminanceSource source ;
158- if (mLimitScanArea ) {
159- source = new PlanarYUVLuminanceSource (
160- imageData , // byte[] yuvData
161- width , // int dataWidth
162- height , // int dataHeight
163- left , // int left
164- top , // int top
165- sWidth , // int width
166- sHeight , // int height
167- false // boolean reverseHorizontal
168- );
169- } else {
170- source = new PlanarYUVLuminanceSource (
171- imageData , // byte[] yuvData
172- width , // int dataWidth
173- height , // int dataHeight
174- 0 , // int left
175- 0 , // int top
176- width , // int width
177- height , // int height
178- false // boolean reverseHorizontal
179- );
180- }
181- if (inverse ) {
182- return new BinaryBitmap (new HybridBinarizer (source .invert ()));
183- } else {
184- return new BinaryBitmap (new HybridBinarizer (source ));
185- }
186- }
18790}
0 commit comments