2

ここで BitmapImage に問題が発生しています。写真を撮ってプレビューし、アップロードできるWP8.1アプリがあります。プレビューは適切に表示され、適切な回転とサイズですが、問題は、それをアップロードするために、storageFile に対応する byte[] を渡すことです。これは、大きくてあまり適切に方向付けられていない画像です。 storageFile から byte[] を取得する前に。

これは、写真をプレビュー/撮影/変換するために使用するスニペットです:)

//Récupère la liste des camèras disponibles
    private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
    {
        DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
            .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);

        if (deviceID != null) return deviceID;
        else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
    }
    //Initialise la caméra
    async private void initCamera()
    {
        //Récupération de la caméra utilisable
        var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
        //Initialise l'objet de capture
        captureManager = new MediaCapture();
        await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
                PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
                AudioDeviceId = string.Empty,
                VideoDeviceId = cameraID.Id
            });

        var maxResolution = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
        await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution);
    }
    //Évènmenet pour le bouton de prise de photo.
    async private void previewpicture_Click(object sender, RoutedEventArgs e)
    {
        //Si la prévisualisation n'est pas lancée
        if (EnPreview == false)
        {
            //On cache les élèments inutilisés.
            tfComment.Visibility = Visibility.Collapsed;
            vbImgDisplay.Visibility = Visibility.Collapsed;
            //On met la prévisualisation dans le bon sens.
            captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
            //On charge la prévisualisation dans l'élèment de l'image de la fenêtre.
            capturePreview.Source = captureManager;
            //On rend la prévisualisation possible.
            capturePreview.Visibility = Visibility.Visible;
            //Démarre le flux de prévisualisation
            await captureManager.StartPreviewAsync();
            //On précise que la prévisualisation est lancée.
            EnPreview = true;
            //On change le label du bouton
            previewpicture.Label = "Prendre photo";
        }
        //Si la prévisualisation est lancée
        else if (EnPreview == true)
        {
            //Créer le fichier de la photo depuis la mémoire interne de l'application
            StorageFile photoFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFirstPhoto.jpg", CreationCollisionOption.ReplaceExisting);
            //Récupère la photo avec le format d'encodage décidé.
            await captureManager.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoFile);

            // we can also take a photo to memory stream
            InMemoryRandomAccessStream memStream = new InMemoryRandomAccessStream();
            await captureManager.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpegXR(), memStream);
            //Debug.WriteLine(memStream.ToString());
            //Récupère l'image dans un objet image qu'on peut mettre dans le container d'affichage
            BitmapImage bitmapToShow = new BitmapImage(new Uri(photoFile.Path));
            //Met l'image dans le container d'affichage
            imgDisplay.Source = bitmapToShow;
            vbImgDisplay.RenderTransform = new RotateTransform() {CenterX = 0.5, CenterY = 0.5, Angle = 90 };
            //Cache la previsualisation
            capturePreview.Visibility = Visibility.Collapsed;
            //Arrête le flux de prévisualisation
            await captureManager.StopPreviewAsync();
            //rend visible les composants
            tfComment.Visibility = Visibility.Visible;
            vbImgDisplay.Visibility = Visibility.Visible;
            //Converti l'image en tableau de byte
            photoByte = await GetByteFromFile(photoFile);
            //Précise qu'on est plus en prévisualisation
            EnPreview = false;
            previewpicture.Label = "Prévisualiser";
        }
    }
    //Converti un storageFile en Byte[]  
    private async Task<byte[]> GetByteFromFile(StorageFile storageFile)
    {
        var stream = await storageFile.OpenReadAsync();

        using (var dataReader = new DataReader(stream))
        {
            var bytes = new byte[stream.Size];
            await dataReader.LoadAsync((uint)stream.Size);
            dataReader.ReadBytes(bytes);

            return bytes;
        }

    }

あなたの助けのために前もってThx!

4

2 に答える 2

0
 //this method will rotate an image any degree
   public static Bitmap RotateImage(Bitmap image, float angle)
    {
        //create a new empty bitmap to hold rotated image

        double radius = Math.Sqrt(Math.Pow(image.Width, 2) + Math.Pow(image.Height, 2));

        Bitmap returnBitmap = new Bitmap((int)radius, (int)radius);

        //make a graphics object from the empty bitmap
        using (Graphics graphic = Graphics.FromImage(returnBitmap))
        {
            //move rotation point to center of image
            graphic.TranslateTransform((float)radius / 2, (float)radius / 2);
            //rotate
            graphic.RotateTransform(angle);
            //move image back
            graphic.TranslateTransform(-(float)image.Width / 2, -(float)image.Height / 2);
            //draw passed in image onto graphics object
            graphic.DrawImage(image, new Point(0, 0));
        }
        return returnBitmap;
    }
于 2019-09-26T20:18:22.187 に答える