如何在Windows Phone上检测相机纵横比

时间:2013-09-24 00:47:14

标签: c# image windows-phone-8 aspect-ratio cameracapturetask

我希望能够确定Windows Phone 8硬件相机的宽高比。根据我的理解,现在有三种设备屏幕尺寸,所以我不确定这是否会影响设备相机的宽高比,或者它们是否是所有手机型号的标准配置?我需要确定相机宽高比的原因是能够在拍摄照片后适当调整图像大小。使用CameraCaptureTask的默认图像对我来说非常大。在我的应用程序中使用CameraCaptureTask,我收到的所有内容都是图像数据流。我手动尝试确定宽高比,尽管使用默认的16:9宽高比的诺基亚920测试,我确定的比例毫无意义,它等于222:125。我正在做的如下

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);
        }
    }

我迄今为止的结果如下。拍摄照片时,根据设备的方向,纵横比为

ASPECT RATIO 16:9显示为 2000x3552 = 125:222 3552x2000 = 222:125 4:3显示为 2448x3264 = 3:4 3264x2448 = 4:3

0 个答案:

没有答案