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

Commit

Permalink
Merge pull request #134 from MSOpenTech/orientation_change
Browse files Browse the repository at this point in the history
Add support for orientation change on Windows
  • Loading branch information
Vladimir Kotikov committed Dec 23, 2015
2 parents cabb67e + 51216ff commit 02c67a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
67 changes: 59 additions & 8 deletions src/windows/BarcodeScannerProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ function findCamera() {
});
}

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

switch (displayOrientation) {
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;
case Windows.Graphics.Display.DisplayOrientations.landscape:
default:
degreesToRotate = 0;
break;
}

if (isMirrored) {
degreesToRotate = (360 - degreesToRotate) % 360
}

return degreesToRotate;
}

module.exports = {

/**
Expand All @@ -79,10 +109,30 @@ module.exports = {
capturePreviewAlignmentMark,
captureCancelButton,
navigationButtonsDiv,
previewMirroring,
closeButton,
capture,
reader;

function updatePreviewForRotation(evt) {
if (!capture) {
return;
}

var ROTATION_KEY = "C380465D-2271-428C-9B83-ECEA3B4A85C1";

var displayInformation = (evt && evt.target) || Windows.Graphics.Display.DisplayInformation.getForCurrentView();
var currentOrientation = displayInformation.currentOrientation;

previewMirroring = previewMirroring || capture.getPreviewMirroring();
var rotDegree = videoPreviewRotationLookup(currentOrientation, previewMirroring);

// rotate the preview video
var videoEncodingProperties = capture.videoDeviceController.getMediaStreamProperties(Windows.Media.Capture.MediaStreamType.VideoPreview);
videoEncodingProperties.properties.insert(ROTATION_KEY, rotDegree);
return capture.setEncodingPropertiesAsync(Windows.Media.Capture.MediaStreamType.VideoPreview, videoEncodingProperties, null);
}

/**
* Creates a preview frame and necessary objects
*/
Expand Down Expand Up @@ -188,7 +238,8 @@ module.exports = {
* Starts stream transmission to preview frame and then run barcode search
*/
function startPreview() {
findCamera(function(id) {
findCamera()
.then(function (id) {
var captureSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
captureSettings.streamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.video;
captureSettings.photoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.videoPreview;
Expand All @@ -212,11 +263,6 @@ 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;
}

capturePreview.src = URL.createObjectURL(capture);
capturePreview.play();
Expand All @@ -225,6 +271,10 @@ module.exports = {
document.body.appendChild(capturePreviewFrame);

setupFocus(controller.focusControl)
.then(function () {
Windows.Graphics.Display.DisplayInformation.getForCurrentView().addEventListener("orientationchanged", updatePreviewForRotation, false);
return updatePreviewForRotation();
})
.then(function () {
return startBarcodeSearch(maxResProps.width, maxResProps.height);
})
Expand Down Expand Up @@ -323,6 +373,9 @@ module.exports = {
*/
function destroyPreview() {

Windows.Graphics.Display.DisplayInformation.getForCurrentView().removeEventListener("orientationchanged", updatePreviewForRotation, false);
document.removeEventListener('backbutton', cancelPreview);

capturePreview.pause();
capturePreview.src = null;

Expand All @@ -335,8 +388,6 @@ module.exports = {

capture && capture.stopRecordAsync();
capture = null;

document.removeEventListener('backbutton', cancelPreview);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/windows/assets/plugin-barcodeScanner.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
top: 50%;
width: 100%;
height: 3px;
background: red
background: red;
z-index: 9999999;
}

Expand Down

2 comments on commit 02c67a2

@SunboX
Copy link
Contributor

@SunboX SunboX commented on 02c67a2 Jan 4, 2016

Choose a reason for hiding this comment

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

Wrong View Finder Orientation on Lumia 950:

img_0016

@jase88
Copy link

@jase88 jase88 commented on 02c67a2 Feb 4, 2016

Choose a reason for hiding this comment

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

Same rotation issue on Lumia 550 with Win10 Mobile

Please sign in to comment.