Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Resolve merge conflicts and address feedback for #62
Browse files Browse the repository at this point in the history
This closes #62, closes #56, closes #42
  • Loading branch information
vladimir-kotikov committed Dec 23, 2015
1 parent d68cdce commit cabb67e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 84 deletions.
122 changes: 38 additions & 84 deletions src/windows/BarcodeScannerProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,71 +40,32 @@ var BARCODE_FORMAT = {
};

/**
* @param {Function<Object>} callback
* Detects the first appropriate camera located at the back panel of device. If
* there is no back cameras, returns the first available.
*
* @returns {Promise<String>} Camera id
*/
function findCamera(callback) {
// Enumerate cameras and add them to the list
var deviceInfo = Windows.Devices.Enumeration.DeviceInformation;
deviceInfo.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture).done(function (cameras) {
var camerasConfigs = cameras.map(function(camera) {
var cameraConfig = {
id: camera.id,
isFrontLocation: false,
isBackLocation: false
};
function findCamera() {
var Devices = Windows.Devices.Enumeration;

if (camera.enclosureLocation !== null) {
cameraConfig.isFrontLocation = camera.enclosureLocation.panel === Windows.Devices.Enumeration.Panel.front;
cameraConfig.isBackLocation = camera.enclosureLocation.panel === Windows.Devices.Enumeration.Panel.back;
}
// Enumerate cameras and add them to the list
return Devices.DeviceInformation.findAllAsync(Devices.DeviceClass.videoCapture)
.then(function (cameras) {

return cameraConfig;
});
if (!cameras || cameras.length === 0) {
throw new Error("No cameras found");
}

var selectedCameraConfig = null;
camerasConfigs.forEach(function(cameraConfig) {
if (cameraConfig.isBackLocation) {
selectedCameraConfig = cameraConfig;
}
var backCameras = cameras.filter(function (camera) {
return camera.enclosureLocation.panel === Devices.Panel.back;
});

if (selectedCameraConfig === null) {
selectedCameraConfig = camerasConfigs[0];
}

callback(selectedCameraConfig.id)
}, function() {
// error happened
// If there is back cameras, return the id of the first,
// otherwise take the first available device's id
return (backCameras[0] || cameras[0]).id;
});
}

/**
* @param {Windows.Graphics.Display.DisplayOrientations} displayOrientation
* @return {Number}
*/
function videoPreviewRotationLookup(displayOrientation) {
var degreesToRotate;

switch (displayOrientation) {
case Windows.Graphics.Display.DisplayOrientations.landscape:
degreesToRotate = 0;
break;
case Windows.Graphics.Display.DisplayOrientations.portrait:
degreesToRotate = 90;
break;
case Windows.Graphics.Display.DisplayOrientations.landscapeFlipped:
degreesToRotate = 180;
break;
case Windows.Graphics.Display.DisplayOrientations.portraitFlipped:
degreesToRotate = 270;
break;
default:
degreesToRotate = 0;
break;
}
return degreesToRotate;
}

module.exports = {

/**
Expand All @@ -126,7 +87,6 @@ module.exports = {
* Creates a preview frame and necessary objects
*/
function createPreview() {
Windows.Graphics.Display.DisplayInformation.getForCurrentView().addEventListener("orientationchanged", updatePreviewForRotation, false);

// Create fullscreen preview
var capturePreviewFrameStyle = document.createElement('link');
Expand Down Expand Up @@ -165,8 +125,6 @@ module.exports = {
[capturePreview, capturePreviewAlignmentMark, navigationButtonsDiv].forEach(function (element) {
capturePreviewFrame.appendChild(element);
});

capture = new Windows.Media.Capture.MediaCapture();
}

function focus(controller) {
Expand Down Expand Up @@ -254,37 +212,33 @@ module.exports = {
var maxResProps = deviceProps[0];

controller.setMediaStreamPropertiesAsync(Windows.Media.Capture.MediaStreamType.videoRecord, maxResProps).done(function () {
// handle portrait orientation
if (Windows.Graphics.Display.DisplayProperties.nativeOrientation == Windows.Graphics.Display.DisplayOrientations.portrait) {
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
capturePreview.msZoom = true;
}
// handle portrait orientation
if (Windows.Graphics.Display.DisplayProperties.nativeOrientation == Windows.Graphics.Display.DisplayOrientations.portrait) {
capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees);
capturePreview.msZoom = true;
}

capturePreview.src = URL.createObjectURL(capture);
capturePreview.play();

// Insert preview frame and controls into page
document.body.appendChild(capturePreview);
document.body.appendChild(capturePreviewAlignmentMark);
document.body.appendChild(captureCancelButton);

// Insert preview frame and controls into page
document.body.appendChild(capturePreviewFrame);

setupFocus(controller.focusControl)
.then(function () {
return startBarcodeSearch(maxResProps.width, maxResProps.height);
})
.done(function (result) {
destroyPreview();
success({
text: result && result.text,
format: result && BARCODE_FORMAT[result.barcodeFormat],
cancelled: !result
document.body.appendChild(capturePreviewFrame);

setupFocus(controller.focusControl)
.then(function () {
return startBarcodeSearch(maxResProps.width, maxResProps.height);
})
.done(function (result) {
destroyPreview();
success({
text: result && result.text,
format: result && BARCODE_FORMAT[result.barcodeFormat],
cancelled: !result
});
}, function (error) {
destroyPreview();
fail(error);
});
}, function (error) {
destroyPreview();
fail(error);
});
});
});
Expand Down
1 change: 1 addition & 0 deletions src/windows/assets/plugin-barcodeScanner.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
width: 100%;
height: 3px;
background: red
z-index: 9999999;
}

.barcode-scanner-app-bar {
Expand Down

2 comments on commit cabb67e

@vladyslav571
Copy link
Contributor

Choose a reason for hiding this comment

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

awesome

@SunboX
Copy link
Contributor

@SunboX SunboX commented on cabb67e Dec 23, 2015

Choose a reason for hiding this comment

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

nice! ❤️

Please sign in to comment.