Skip to content

Commit

Permalink
fix(barcoderead): update barcode read internally
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Dec 26, 2019
1 parent be9a462 commit 5bc94ae
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 244 deletions.
93 changes: 10 additions & 83 deletions src/hooks/useBarcodeReadAndroid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useCallback } from 'react';
import { PixelRatio } from 'react-native';
import { BarCodeType, Point, RNCamera, Size } from 'react-native-camera';
import useBarcodeFinder from './useBarcodeFinder';
import useInternalBarcodeRead from './useInternalBarcodeRead';

export default (
isFocused: boolean,
Expand All @@ -19,90 +17,19 @@ export default (
processingReadBarcode,
} = useBarcodeFinder(dataProcessor, onScannedData);

const _onBarcodeRead = useCallback(
(event: {
data: string;
bounds:
| { width: number; height: number; origin: Array<Point<string>> }
| { origin: Point<string>; size: Size<string> };
type: keyof BarCodeType;
}) => {
if (!isFinderBoundingInitialized) {
return;
}

const _bounds = event.bounds as {
width: number;
height: number;
origin: Point<string>[];
};
const _pointBounds = _bounds.origin.map(point => ({
x: Number(point.x) / PixelRatio.get(),
y: Number(point.y) / PixelRatio.get(),
}));

const _insideBox = (point: { x: number; y: number }) => {
const { x, y } = point;
return (
x >= finderX &&
x <= finderX + finderWidth &&
y >= finderY &&
y <= finderY + finderHeight
);
};

/**
* 0 --------------- 2
* | PDF417 |
* | /////////////// |
* 1 --------------- 3
*/
if (event.type === RNCamera.Constants.BarCodeType.pdf417) {
const [topLeft, bottomLeft, topRight, bottomRight] = _pointBounds;
if (
_insideBox(topLeft) &&
_insideBox(bottomLeft) &&
_insideBox(topRight) &&
_insideBox(bottomRight)
) {
processingReadBarcode(event.data);
return;
}
}

/**
* 2 ------ 3
* |
* | QR Code
* |
* 1 ------ 0
*/
if (event.type === RNCamera.Constants.BarCodeType.qr) {
const [bottomRight, bottomLeft, topLeft, topRight] = _pointBounds;
if (
_insideBox(bottomRight) &&
_insideBox(bottomLeft) &&
_insideBox(topLeft) &&
_insideBox(topRight)
) {
processingReadBarcode(event.data);
return;
}
}
},
[
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
]
const onBarcodeRead = useInternalBarcodeRead(
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
processingReadBarcode
);

return {
barcodeRead,
onBarcodeRead: _onBarcodeRead,
onBarcodeRead,
onBarcodeFinderLayoutChange,
};
};
47 changes: 10 additions & 37 deletions src/hooks/useBarcodeReadIOS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCallback } from 'react';
import { BarCodeType, Point, Size } from 'react-native-camera';
import useBarcodeFinder from './useBarcodeFinder';
import useInternalBarcodeRead from './useInternalBarcodeRead';

export default (
isFocused: boolean,
Expand All @@ -18,45 +17,19 @@ export default (
processingReadBarcode,
} = useBarcodeFinder(dataProcessor, onScannedData);

const _onBarcodeRead = useCallback(
(event: {
data: string;
bounds:
| { width: number; height: number; origin: Array<Point<string>> }
| { origin: Point<string>; size: Size<string> };
type: keyof BarCodeType;
}) => {
if (!isFinderBoundingInitialized) {
return;
}

const {
origin: { x, y },
size: { width, height },
} = event.bounds as { origin: Point<string>; size: Size<string> };
if (
Number(x) >= finderX &&
Number(x) + Number(width) <= finderX + finderWidth &&
Number(y) >= finderY &&
Number(y) + Number(height) <= finderY + finderHeight
) {
processingReadBarcode(event.data);
return;
}
},
[
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
]
const onBarcodeRead = useInternalBarcodeRead(
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
processingReadBarcode
);

return {
barcodeRead,
onBarcodeRead: _onBarcodeRead,
onBarcodeRead,
onBarcodeFinderLayoutChange,
};
};
95 changes: 10 additions & 85 deletions src/hooks/useCustomBarcodeReadAndroid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useCallback } from 'react';
import { PixelRatio } from 'react-native';
import { BarCodeType, Point, RNCamera, Size } from 'react-native-camera';
import { CustomBarcodeRead, CustomBarcodeReadCallback } from '../interfaces';
import useBarcodeFinder from './useBarcodeFinder';
import useInternalBarcodeRead from './useInternalBarcodeRead';

export default (
isFocused: boolean,
Expand All @@ -22,87 +20,6 @@ export default (
processingReadBarcode,
} = useBarcodeFinder(dataProcessor, onScannedData, customBarcodeRead);

const _onBarcodeRead = useCallback(
(event: {
data: string;
bounds:
| { width: number; height: number; origin: Array<Point<string>> }
| { origin: Point<string>; size: Size<string> };
type: keyof BarCodeType;
}) => {
if (!isFinderBoundingInitialized) {
return;
}

const _bounds = event.bounds as {
width: number;
height: number;
origin: Point<string>[];
};
const _pointBounds = _bounds.origin.map(point => ({
x: Number(point.x) / PixelRatio.get(),
y: Number(point.y) / PixelRatio.get(),
}));

const _insideBox = (point: { x: number; y: number }) => {
const { x, y } = point;
return (
x >= finderX &&
x <= finderX + finderWidth &&
y >= finderY &&
y <= finderY + finderHeight
);
};

/**
* 0 --------------- 2
* | PDF417 |
* | /////////////// |
* 1 --------------- 3
*/
if (event.type === RNCamera.Constants.BarCodeType.pdf417) {
const [topLeft, bottomLeft, topRight, bottomRight] = _pointBounds;
if (
_insideBox(topLeft) &&
_insideBox(bottomLeft) &&
_insideBox(topRight) &&
_insideBox(bottomRight)
) {
processingReadBarcode(event.data);
return;
}
}

/**
* 2 ------ 3
* |
* | QR Code
* |
* 1 ------ 0
*/
if (event.type === RNCamera.Constants.BarCodeType.qr) {
const [bottomRight, bottomLeft, topLeft, topRight] = _pointBounds;
if (
_insideBox(bottomRight) &&
_insideBox(bottomLeft) &&
_insideBox(topLeft) &&
_insideBox(topRight)
) {
processingReadBarcode(event.data);
return;
}
}
},
[
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
]
);

return {
barcodeRead,
onBarcodeRead: customBarcodeReadCallback
Expand All @@ -116,7 +33,15 @@ export default (
isFinderBoundingInitialized,
processingReadBarcode
)
: _onBarcodeRead,
: useInternalBarcodeRead(
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
processingReadBarcode
),
onBarcodeFinderLayoutChange,
};
};
49 changes: 10 additions & 39 deletions src/hooks/useCustomBarcodeReadIOS.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CustomBarcodeRead, CustomBarcodeReadCallback } from 'interfaces';
import { useCallback } from 'react';
import { BarCodeType, Point, Size } from 'react-native-camera';
import useBarcodeFinder from './useBarcodeFinder';
import useInternalBarcodeRead from './useInternalBarcodeRead';

export default (
isFocused: boolean,
Expand All @@ -21,42 +20,6 @@ export default (
processingReadBarcode,
} = useBarcodeFinder(dataProcessor, onScannedData, customBarcodeRead);

const _onBarcodeRead = useCallback(
(event: {
data: string;
bounds:
| { width: number; height: number; origin: Array<Point<string>> }
| { origin: Point<string>; size: Size<string> };
type: keyof BarCodeType;
}) => {
if (!isFinderBoundingInitialized) {
return;
}

const {
origin: { x, y },
size: { width, height },
} = event.bounds as { origin: Point<string>; size: Size<string> };
if (
Number(x) >= finderX &&
Number(x) + Number(width) <= finderX + finderWidth &&
Number(y) >= finderY &&
Number(y) + Number(height) <= finderY + finderHeight
) {
processingReadBarcode(event.data);
return;
}
},
[
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
]
);

return {
barcodeRead,
onBarcodeRead: customBarcodeReadCallback
Expand All @@ -70,7 +33,15 @@ export default (
isFinderBoundingInitialized,
processingReadBarcode
)
: _onBarcodeRead,
: useInternalBarcodeRead(
isFocused,
isFinderBoundingInitialized,
finderX,
finderY,
finderWidth,
finderHeight,
processingReadBarcode
),
onBarcodeFinderLayoutChange,
};
};
Loading

0 comments on commit 5bc94ae

Please sign in to comment.