Search code examples
c#apache-flexcamerabitmapdatadistriqt

Saving 32-bit Unsigned Byte Array to File System


I am trying to save a byte array (of an image) to the file system using C# and it isn't working using either method I have tried, here is code sample 1:

string path = @"c:\ppd" + g.ToString() + ".jpg";
        using (MemoryStream inputStream = new MemoryStream(image))
        {
            Image returnImage = Image.FromStream(inputStream);
            returnImage.Save(path, ImageFormat.Jpeg);
        }

With this code I get 'Parameter is not valid' when sending the data to the service. So that's obviously the wrong code for the array, so I hit google and came up with this code:

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

This code writes the file to the file system, but when you try to open it it helpfully says 'This is not a valid bitmap file, or it's format is not currently supported.

We get the byteArray from Flex using:

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

And the bitmapData comes from the Android camera (via the Distriqt Camera ANE). The image displays fine in the app, but I have no idea where to go next with this, please help.

EDIT: The code in Flex to get the byteArray is:

<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))))}"/>

PictureEvent takes 3 parameters, type:String, id:int = 0, image:ByteArray = null.

SECOND EDIT: Here is the code that creates the bitmapData:

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

THIRD EDIT: I has done some testing and discovered that this code should work, it is a variation on the first block above that throws 'Parameter is not valid'

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

So now I am sure the error is with the byteArray, but I genuinely cannot see what I am doing wrong. I have the bitmapData and it is valid, and it looks like I am collecting the data I need, the written file is nearly 700Kb in size, but something is wrong. If someone could tell me what is wrong on the Flex side of this equation I would be forever grateful. I have the BitmapImage on the display list, it is bound to the model correctly and I have tried different ways of pointing to the source and drawing up the rectangle.


Solution

  • Ok, so @Lorek was right, the byteArray just contains pixel data and no formatting information. We don't know why this is the case but based on that knowledge and a little digging in the database I realised that this has been the situation for a while and I needed a solution that would solve it. So in answer to the question 'How do you convert pixel data from a byteArray into an image?' I came up with this, still need a little help, but basically this solves the issue I am having (more after the code)

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

    So I change the Rectangle size in Flex so I knew what I was working with, and posted the btyeArray to this method, which beautifully writes out a PNG for me.

    It's slightly blue, well it's really blue and I am not quite sure why, take a look: http://ec2-52-89-85-67.us-west-2.compute.amazonaws.com/imagedata/ppd/3898ae89-e4e0-4d03-97d7-dac4e3b618d5.png It is supposed to be black: http://ec2-52-89-85-67.us-west-2.compute.amazonaws.com/imagedata/ppd/capture.png

    So any of you .Net people know what happened and can advise me (I heard somewhere that I need to switch the order of the colors so instead of rgb it gets switched around, but I have no idea how or why this would be the issue).

    EDIT Here is the solution to that, it's slow, it's pointless and it takes too long: http://www.codeproject.com/Articles/2056/Image-Processing-for-Dummies-with-C-and-GDI-Part-3

    Or if maybe we can solve the actual Flex issue that's be great although I have given up all hope of SO being useful for Flex support any more

    Thanks you everyone for your patience.