AngularJS:相机捕获后旋转图像

时间:2015-09-17 11:44:43

标签: angularjs ionic

我正在使用AngularJS技术在离子框架上使用我的Android手机拍照。这是我在index.html中的代码审查

<!DOCTYPE html>
<html>
  <head>

      <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>
    <script src="js/ng-cordova.min.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">
<ion-content ng-controller="ExampleController">
    <img ng-show="imgURI !== undefined" ng-src="{{imgURI}}">
    <img ng-show="imgURI === undefined" ng-src="http://placehold.it/300x300">
    <button class="button" ng-click="takePicture()">Take Picture</button>

</ion-content>
  </body>
</html>

这是我在app.js中的代码

.controller("ExampleController", function($scope, $cordovaCamera) {

    $scope.takePicture = function() {
        var options = { 
            quality : 75, 
            destinationType : Camera.DestinationType.DATA_URL, 
            sourceType : Camera.PictureSourceType.CAMERA, 
            allowEdit : true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function(imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
        }, function(err) {
            // An error occured. Show a message to the user
        });
    }
});

我的挑战是旋转图像。如果它是Java android我可以使用Exif方向轻松旋转图像,但在这种情况下,我不知道如何使用angularjs执行旋转90度。请帮帮我。这是我的第一次!!!

3 个答案:

答案 0 :(得分:1)

correctOrientation: true

targetWidth小于700

答案 1 :(得分:0)

您可以使用画布旋转,如http://www.ajaxblender.com/howto-rotate-image-using-javascript-canvas.html

所示

这将是这样的:

.controller("ExampleController", function($scope, $cordovaCamera) {

    $scope.takePicture = function() {
        var options = { 
            quality : 75, 
            destinationType : Camera.DestinationType.DATA_URL, 
            sourceType : Camera.PictureSourceType.CAMERA, 
            allowEdit : true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function(imageData) {
            var img = angular.element('img'),
                canvas = angular.element('canvas'),
                context,
                ch,
                cw,
                cx = 0, 
                cy = 0;

            $scope.imgURI = "data:image/jpeg;base64," + imageData;
            context = canvas.getContext('2d');
            cw = img.width;
            ch = img.height;
            canvas.width = cw;
            canvas.height = ch;

            // draw image to canvas
            context.drawImage(img, cx, cy);

            // calculate coordinates for image
            cw = img.height;
            ch = img.width;
            cy = img.height * (-1);

            // rotate 90 degrees
            canvas.setAttribute('width', cw);
            canvas.setAttribute('height', ch);
            context.rotate(degree * Math.PI / 180);
            context.drawImage(img, cx, cy);

        }, function(err) {
            // An error occured. Show a message to the user
        });
    }
});

答案 2 :(得分:0)

我能够通过将其添加到app.js控制器摄像头选项

来解决我的问题

使用方向选项。

它就像魅力一样。