1 Reply Latest reply on Sep 14, 2012 4:55 AM by Rajkumar_S

    Loader changes the pixel value after loading as bitmap

    Rajkumar_S

      Hi all,

      My aim is to load two images and compare its color information pixel by pixel. For this purpose i used "flash.display.Loader" class to load the images, and once load completes i'll typecast the loaded content to bitmap object (loaderObj.content as Bitmap). Finally i'll get the bitmapdata from the bitmap of both the images and compare the pixels.

       

      Now the problem is the color information of the original image and the loaded bitmap is slightly different. For example the color value of pixels of the attached image image1.jpg is 0xDDDDDD for complete 1366x768. And for the same image when it is loaded as bitmap the color value of pixels are different from the original value (For some pixels only, not all pixels are different).

       

      I’ve given the source code and sample images.

       

      Please can anyone help me out how to resolve this issue?

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

                                                           xmlns:s="library://ns.adobe.com/flex/spark"

                                                           xmlns:mx="library://ns.adobe.com/flex/mx"

                                                           width="1376" height="800"

                                                           showStatusBar="false">

                <fx:Declarations>

                          <!-- Place non-visual elements (e.g., services, value objects) here -->

                </fx:Declarations>

       

                <fx:Script>

                          <![CDATA[

                                    private function loadBtn_ClickHandler():void{

                                              var file:File = new File(File.desktopDirectory.nativePath);

                                              file.addEventListener(Event.SELECT, onFileSelectionHandler);

                                              file.browseForOpen("Open an image",[new FileFilter("Images", "*.jpg;*.gif;*.png;*.jpeg")]);

                                    }

                                    private function onFileSelectionHandler(ev:Event):void{

                                              var loadedFilePath:String = (ev.target as File).nativePath;

                                              loadImageAsBitmap(loadedFilePath);

                                    }

                                    /* Load image as bitmap using Loader */

                                    private function loadImageAsBitmap(url:String):void{

                                              var loader:Loader = new Loader();

                                              // load content as bitmap

                                              loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onContentLoadComplete);

                                              loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onContentLoadFail);

                                              loader.load(new URLRequest(url));

                                    }

                                    private function onContentLoadComplete(ev:Event):void{

                                              var loaderInfo:LoaderInfo = ev.target as LoaderInfo;

                                              var bitmap:Bitmap = loaderInfo.content as Bitmap;

                                              printPixelValue(bitmap.bitmapData);

                                              container_ID.addChild(bitmap);

                                    }

                                    private function onContentLoadFail(ioe:IOErrorEvent):void{

                                              trace("Image Load Failed");

                                    }

                                    private function printPixelValue(bmData:BitmapData):void{

                                              var initPixelValue:Number = -1;

                                              //for (var countI:int = 0; countI < bmData.height; countI++) {

                                              var countI:int = 1;

                                              for (var countJ:int = 0; countJ < bmData.width; countJ++) {

                                                        var pixelValue:Number = bmData.getPixel32(countJ, countI);

                                                        if(pixelValue != initPixelValue){

                                                                  initPixelValue = pixelValue;

                                                                  // Print the pixel value only for one row

                                                                  trace("Pixel Value at " + countI + "x" + countJ + " is: " + pixelValue);

                                                        }

                                              }

                                              trace("Pixel Value at " + countI + "x" + countJ + " is: " + pixelValue);

                                              //}

                                    }

                          ]]>

                </fx:Script>

                <s:Button id="loadBtn_ID" label="Load!" click="loadBtn_ClickHandler()"

                                      x="5" y="5" height="20"/>

                <mx:UIComponent id="container_ID" x="5" y="30" width="1366" height="768"/>

      </s:WindowedApplication>

       

       

      Sample Image:

      main.jpg