将32位无符号字节数组保存到文件系统

时间:2015-10-09 13:30:10

标签: c# flex camera bitmapdata distriqt

我正在尝试使用C#将文件系统的一个字节数组(图像)保存到文件系统中,并且使用我尝试的任何一种方法都无法正常工作,这里是代码示例1:

string path = @"c:\ppd\" + g.ToString() + ".jpg";
        using (MemoryStream inputStream = new MemoryStream(image))
        {
            using (Stream file = File.Create(path))
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    file.Write(buffer, 0, len);
                }
            }
        }

使用此代码,我将数据发送到服务时获得“参数无效”。所以这显然是数组的错误代码,所以我点击谷歌并想出了这段代码:

bitmapData.getPixels(0, 0, bitmapData.width, bitmapData.height);

此代码将文件写入文件系统,但是当您尝试打开它时,它会有用地说“这不是有效的位图文件,或者当前不支持它的格式。”

我们使用以下方法从Flex获取byteArray:

<s:BitmapImage id="bitmapImage" width="100%"
                 maxHeight="{this.height/2}" 
                 source="{profileModel.captureBitmapPreview}"
                 />

<s:Button click="{dispatchEvent(new PictureEvent
                      (PictureEvent.UPLOAD,
                      0, pictureModel.captureBitmapPreview.getPixels(new Rectangle(0,0,pictureModel.captureBitmapPreview.width,pictureModel.captureBitmapPreview.height))))}"/>
/* this is the alternative which still doesn't work **/
<s:Button click="{dispatchEvent(new PictureEvent
                      (PictureEvent.UPLOAD,
                      0, bitmapImage.getPixels(new Rectangle(0,0,bitmapImage.width,bitmapImage.height))))}"/>

bitmapData来自Android相机(通过Distriqt Camera ANE)。图片在应用程序中显示正常,但我不知道下一步该怎么做,请帮忙。

编辑: Flex中获取byteArray的代码是:

private function camera_capturedImageHandler(evt:CameraDataEvent):void
    {
        Camera.instance.setPresetMode(CameraMode.PRESET_MEDIUM);
        Camera.instance.addEventListener(CameraEvent.VIDEO_FRAME, camera_videoFrameHandler, false, 0, true);

        if (_captureBitmapData.width != evt.data.width || _captureBitmapData.height != evt.data.height)
        {
            _captureBitmapData = new BitmapData(evt.data.width, evt.data.height, false);
        }
        _captureBitmapData.draw(evt.data);
        var tbd:BitmapData = new BitmapData(FlexGlobals.topLevelApplication.width, FlexGlobals.topLevelApplication.height, false)
        tbd = applyOrientation(_bitmapData, "6");
        profileModel.captureBitmapPreview.copyPixels(tbd, new Rectangle(0, ((FlexGlobals.topLevelApplication.height/2)-(FlexGlobals.topLevelApplication.height/4)), FlexGlobals.topLevelApplication.width, FlexGlobals.topLevelApplication.height/2), new Point(0, 0));
    }

PictureEvent有3个参数,类型:String,id:int = 0,image:ByteArray = null。

SECOND EDIT:这是创建bitmapData的代码:

ImageConverter imageConverter = new ImageConverter();
        Bitmap bm = (Bitmap)imageConverter.ConvertFrom(image);

        if (bm != null && (bm.HorizontalResolution != (int)bm.HorizontalResolution || bm.VerticalResolution != (int)bm.VerticalResolution))
        {
            bm.SetResolution((int)(bm.HorizontalResolution + 0.5f),(int)(bm.VerticalResolution + 0.5f));
        }

        bm.Save(path, ImageFormat.Jpeg);

第三次编辑: 我做了一些测试,发现这段代码应该有效,它是上面第一个块的变种,抛出'参数无效'

#navbar-iframe 
{
  background: transparent !important;  
}

.site-header h1, 
.site-header h2 
{
  text-align: center;
}

.header-inner .Header .titlewrapper, 
.header-inner .Header .descriptionwrapper 
{
  padding-left: 50;
  padding-right: 50;
  text-align: center;
}

所以现在我确定错误在于byteArray,但我真的看不出我做错了什么。我有bitmapData,它是有效的,看起来我正在收集我需要的数据,写入的文件大小接近700Kb,但有些不对劲。 如果有人能告诉我这个等式的Flex方面有什么问题,我将永远感激不尽。我在显示列表上有BitmapImage,它正确绑定到模型,我尝试了不同的指向源和绘制矩形的方法。

1 个答案:

答案 0 :(得分:0)

好的,所以@Lorek是对的,byteArray只包含像素数据而没有格式化信息。我们不知道为什么会这样,但基于这些知识并在数据库中进行了一些挖掘,我意识到这已经存在了一段时间,我需要一个可以解决它的解决方案。 因此,回答问题&#39;如何将像素数据从byteArray转换为图像?&#39;我想出了这个,仍然需要一些帮助,但基本上这解决了我遇到的问题(更多代码之后)

Bitmap bitmap = new Bitmap(150, 150, PixelFormat.Format32bppRgb);
BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(image, 0, pNative, image.Length);
bitmap.UnlockBits(bmData);
bitmap.Save(path, ImageFormat.Png);

所以我改变了Flex中的Rectangle大小,所以我知道我在使用什么,并将btyeArray发布到这个方法,它为我精美地写了一个PNG。

它略带蓝色,它真的很蓝,我不太清楚为什么,看一看: http://ec2-52-89-85-67.us-west-2.compute.amazonaws.com/imagedata/ppd/3898ae89-e4e0-4d03-97d7-dac4e3b618d5.png 它应该是黑色的: http://ec2-52-89-85-67.us-west-2.compute.amazonaws.com/imagedata/ppd/capture.png

所以你们中的任何一个.Net的人都知道发生了什么并且可以告诉我(我听说我需要切换颜色的顺序而不是rgb它会被切换,但我不知道这是怎么回事是问题)。

编辑以下是解决方案,它很慢,没有意义,而且耗时太长:http://www.codeproject.com/Articles/2056/Image-Processing-for-Dummies-with-C-and-GDI-Part-3

或者,如果我们可以解决实际的Flex问题,那就太好了,尽管我已经放弃了所有希望SO对Flex支持更有用

感谢大家的耐心等待。