0

I would like to be able to determine the aspect ratio of a Windows Phone 8 hardware camera. From what I understand there are three device screen sizes now, so I'm not sure if this affects the device camera aspect ratios or if they are standard across all phone models? The reason I need to determine the camera aspect ratio is to be able to resize images appropriately once a picture is taken. The default images using CameraCaptureTask are much to large for my purposes. With using the CameraCaptureTask within my application, all I receive back is the stream of image data. I was manually trying to determine the aspect ratio from this, although with testing with a Nokia 920 in the default 16:9 aspect ratio, the ratio I determined makes no sense, it equals 222:125. What I am doing is as follows

MainPage.xaml.cs

public int GCD(int a, int b)
    {
        while (a != 0 && b != 0)
        {
            if (a > b)
                a %= b;
            else
                b %= a;
        }
        if (a == 0)
            return b;
        else
            return a;
    }

private void cameraTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            BitmapImage bmi = new BitmapImage();
            bmi.SetSource(e.ChosenPhoto);
            //MessageBox.Show(bmi.PixelWidth.ToString() + "x" + bmi.PixelHeight.ToString());

            var gcd = GCD(bmi.PixelWidth, bmi.PixelHeight);
            var result = string.Format("{0}:{1}", bmi.PixelWidth / gcd, bmi.PixelHeight / gcd);

            WriteableBitmap wb = new WriteableBitmap(bmi);
            //WriteableBitmap wb;
            Stream stream = new MemoryStream();

            switch (result)
            {
                case "3:4":
                    //wb = new WriteableBitmap(480, 640);
                    //wb.SetSource(e.ChosenPhoto); 
                    wb.SaveJpeg(stream, 480, 640, 0, 100);
                    break;
                case "4:3":
                    //wb = new WriteableBitmap(640, 480);
                    wb.SaveJpeg(stream, 640, 480, 0, 100);
                    break;
                case "9:16":
                    //wb = new WriteableBitmap(448, 800);
                    wb.SaveJpeg(stream, 448, 800, 0, 100);
                    break;
                case "16:9":
                    //wb = new WriteableBitmap(800, 448);
                    wb.SaveJpeg(stream, 800, 448, 0, 100);
                    break;
                default:
                    wb = null;
                    return;
            }

            stream.Seek(0, SeekOrigin.Begin);
            //stream.Dispose();

            var capturedPicture = new CapturedPicture(e.OriginalFileName, stream);
        }
    }

My results thus far are as follows. Depending on the orientation of the device when the picture is taken, the aspect ratio are

ASPECT RATIO 16:9 shows as 2000x3552 = 125:222 3552x2000 = 222:125 4:3 shows as 2448x3264 = 3:4 3264x2448 = 4:3

4

0 回答 0