Here is what I came up with. It checks to see if the Jpg has the final necessary bytes. I'm open to better suggestions, but this covers what I need for now. It worked with the few corrupt images I had to test.

I put the same thing in a blog post on our site: 
http://blog.mediarain.com/2010/02/checking-in-flexas3-for-corrupt-jpgs-invalid-images/

protected function isValid(bytes:ByteArray):Boolean
                {
                        var toReturn:Boolean = true;

//take the file name and check it against jpg extensions to make sure it is a jpg
                        var namePieces:Array = file.name.split(".");
//I'm getting the file extension this lame way, bc for some reason file.type is null
                        var extension:String = namePieces[namePieces.length -1];
                                                
                        //if it is indeed a jpeg then we want to check the 
validity
if(extension.toLowerCase() == 'jpg' || extension.toLowerCase() == 'jpeg')
                        {
                                //move to the second to last position in the 
bytearray
                                bytes.position = bytes.bytesAvailable - 2;
                                
                                //get the last two bytes
                                var secondToLastByte:uint = 
bytes.readUnsignedByte();
                                var lastByte:uint = bytes.readUnsignedByte();
                                
//if the last two bytes don't match the EOI segment header at the end of the file
                                //then we know it is invalid
                                if(secondToLastByte != 255 || lastByte != 217)
                                {
                                        toReturn = false;
                                }
                        }
                        
                        return toReturn;
                }

Hopefully this can help somebody else as well.


On Feb 25, 2010, at 10:47 AM, brycebarrand wrote:

I'm working on an app where I want to check in Flex, before uploading, if a jpg is corrupt or valid.

Before I reinvent the wheel, I was wondering if someone had something like this already written. I checked the forums and didn't see anything...

In the meantime I'll start working on it, and post what I come up with :)



Reply to sender | Reply to group
Messages in this topic (1)

Reply via email to