vision-camera-mrz-scanner
Version:
VisionCamera Frame Processor Plugin to detect and read MRZ data from passports using MLKit Text Recognition.
92 lines (87 loc) • 3.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.boundingBoxAdjustToView = void 0;
var _reactNative = require("react-native");
/**
* It takes a frame and a view, and returns an object with two functions: adjustPoint and adjustRect
* @param {Dimensions} frame - Dimensions - the dimensions of the video frame
* @param {Dimensions} view - Dimensions
* @returns An object with two functions.
* @resource https://github.com/bglgwyng/FrameProcessorExample/blob/e8e99d58c878d4dce9a8adf74a7447d253be93ab/adjustToView.ts#L21
*/
const boundingBoxAdjustToView = _reactNative.Platform.OS === 'ios' ? (frame, view) => {
const widthRatio = view.width / frame.width;
const heightRatio = view.height / frame.height;
return {
adjustPoint: point => ({
x: point.x * widthRatio,
y: point.y * heightRatio
}),
adjustRect(rect) {
return {
left: rect.left * widthRatio,
top: rect.top * heightRatio,
width: rect.width * widthRatio,
height: rect.height * heightRatio
};
}
};
} : (frame, view, landscape, verticalPadding, horizontalPadding) => {
const {
width,
height
} = view;
/* Calculating the aspect ratio of the view. */
const aspectRatio = width / height;
const frameWidth = frame.width;
const frameHeight = frame.height;
const frameAspectRatio = frameWidth / frameHeight;
/* Setting the widthRatio, heightRatio, offsetX, and offsetY to 0. */
let widthRatio;
let heightRatio;
let offsetX = 0;
let offsetY = 0;
const verticalCropPadding = verticalPadding || 0;
const horizontalCropPadding = horizontalPadding || 0;
// /* Calculating the ratio of the frame to the view. */
if (!landscape) {
if (frameAspectRatio < aspectRatio) {
widthRatio = width / frameWidth;
const croppedFrameHeight = aspectRatio * frameWidth;
offsetY = (frameHeight - croppedFrameHeight) / 2;
heightRatio = height / croppedFrameHeight;
} else {
heightRatio = height / frameHeight;
const croppedFrameWidth = aspectRatio * frameHeight;
offsetX = (frameWidth - croppedFrameWidth) / 2;
widthRatio = width / croppedFrameWidth;
}
} else {
const croppedFrameWidth = aspectRatio * frameHeight;
const croppedFrameHeight = aspectRatio * frameWidth;
if (!landscape) {
offsetX = (frameWidth - croppedFrameWidth) / 2;
} else {
offsetY = (frameHeight - croppedFrameHeight) / 2;
}
heightRatio = height / frameHeight;
widthRatio = width / croppedFrameWidth;
}
/* Returning an object with two functions. */
return {
adjustPoint: point => ({
x: (point.x - offsetX) * widthRatio,
y: (point.y - offsetY) * heightRatio
}),
adjustRect: rect => ({
top: (rect.top - offsetY - verticalCropPadding) * heightRatio,
left: (rect.left - offsetX - horizontalCropPadding) * widthRatio,
height: (rect.height + verticalCropPadding) * heightRatio,
width: (rect.width + horizontalCropPadding) * widthRatio
})
};
};
exports.boundingBoxAdjustToView = boundingBoxAdjustToView;
//# sourceMappingURL=boundingBoxAdjustToView.js.map