A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: getPixel on a loaded .jpeg instance

  1. #1
    Junior Member
    Join Date
    Jun 2005
    Posts
    29

    Question getPixel on a loaded .jpeg instance

    i want to:

    1. load a .jpeg into a movieClip (by placing a mc on stage / by create command)
    <<< work perfect

    2. i want to get the a pixel color of this movieClip
    structue: jpeg > load into a empty mc > use getPixel to get the mc pixel color
    <<<doesn't work at all ...
    something wrong on logic or what ? ...

    anyone can help a bit?

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here is something that can help you further. The principle is the same whether you load an image or draw something.

    http://www.flash8.flashscript.biz/st...ore_image.html
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    thx for reply

    but this method take a lot of time for vector>data>vector

    i want to load jpeg>getpixel, should be a second process

    can it be achieve ?

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to create BitmapData first of your loaded image in the movieclip and use the draw method. That is why I gave you the link, because the principle is the same.

    snap = new BitmapData (obj._width, obj._height);
    snap.draw (obj, new Matrix ());
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    i've read the tutorial before thx
    seems mine is different

    here's my code:
    Code:
    import flash.display.BitmapData;
    import flash.geom.ColorTransform
    import flash.geom.Matrix;
    
    Stage.scaleMode = 'noScale';
    Mouse.hide();
    
    pixData_xml = new XML();
    pixData_xml.onLoad = startGetPix;
    pixData_xml.load("pixData.xml");
    pixData_xml.ignoreWhite = true;
    
    
    function startGetPix(success){
    	imgPath = pixData_xml.childNodes[0].firstChild.attributes.imgURL
    	loadMovie(imgPath, img_mc) 
    	getPix();
    }
    
    function getPix(){
    var image_bitmap = new flash.display.BitmapData(img_mc._width, img_mc._height);
    image_bitmap.draw(img_mc, new Matrix());
    }
    
    onMouseMove = function(){
    	picker_mc._x = _xmouse;
    	picker_mc._y = _ymouse;
    	var curr_color:Number = image_bitmap.getPixel(img_mc._xmouse, img_mc._ymouse);
    	var preview_colortansform  = new flash.geom.ColorTransform();
    	preview_colortansform.rgb = curr_color;
    	color_mc.transform.colorTransform = preview_colortansform;
    	hex_txt.text = "0x" + curr_color.toString(16).toUpperCase();
    	updateAfterEvent();
    }
    my logic:
    1) load imgURL data from .xml <<< checked, no problem
    2) load this jpeg into the img_mc <<< checked, no problem
    3) create a bitmap instance of img_mc <<< dun know ...
    4) create color picker to check the color on _x/_ymouse <<< work if i attached jpeg from library

    what's wrong here?

  6. #6
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    and i also try this:

    Code:
    this.createEmptyMovieClip("holder_mc", this.getNextHighestDepth()); 
    this.createEmptyMovieClip("show_mc",this.getNextHighestDepth()); 
    loader = new MovieClipLoader(); 
    loader.addListener(this); 
    loader.loadClip("images/level_1o.jpg", holder_mc); 
    function onLoadInit() { 
        myBitmap = new BitmapData(holder_mc._width, holder_mc._height, true, 0x00FFFFFF)
        myBitmap.draw(holder_mc); 
        holder_mc.removeMovieClip();  
        show_mc.attachBitmap(myBitmap,this.getNextHighestDepth()); 
    } 
    
    function onLoadError(){ 
        holder_mc.removeMovieClip(); 
    }
    still doesn't work for the color Picker ...

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    What is the colorpicker? Never heard that in Flash 8.
    - The right of the People to create Flash movies shall not be infringed. -

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Another important point. When your image loads the width and height of the movieclip will still be 0. That could be the problem, since it takes time to load the image.
    You need to use ASBroadcaster or EventDispatcher. Principle is shown here:
    http://board.flashkit.com/board/showthread.php?t=698774
    Just change to your script.
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    Quote Originally Posted by cancerinform
    What is the colorpicker? Never heard that in Flash 8.
    i mean getPixel() like this:

    picker_mc._x = _xmouse;
    picker_mc._y = _ymouse;
    var curr_color:Number = image_bitmap.getPixel(img_mc._xmouse, img_mc._ymouse);
    var preview_colortansform = new flash.geom.ColorTransform();
    preview_colortansform.rgb = curr_color;
    color_mc.transform.colorTransform = preview_colortansform;
    hex_txt.text = "0x" + curr_color.toString(16).toUpperCase();

  10. #10
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Look at my other comment. That needs to be repaired first.
    - The right of the People to create Flash movies shall not be infringed. -

  11. #11
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    i can now getPixel() from a external jpg
    but what i finallly want to achieve, is getPixel32()

    i change the code, but the alpha value is always FF(255)
    i don't know why ...
    can anyone see the problem?

    Code:
    import flash.display.BitmapData; 
    import flash.geom.ColorTransform; 
    import flash.geom.Matrix; 
    
    Stage.scaleMode = 'noScale'; 
    Mouse.hide(); 
    
    pixData_xml = new XML(); 
    pixData_xml.onLoad = startGetPix; 
    pixData_xml.load("pixData.xml"); 
    pixData_xml.ignoreWhite = true; 
    
    picListener = new Object(); 
    picLoader = new MovieClipLoader(); 
    picListener.onLoadInit = function(target_mc:MovieClip) { 
       getPix(); 
    }; 
    picLoader.addListener(picListener); 
    
    function startGetPix(success) { 
       imgPath = pixData_xml.childNodes[0].firstChild.attributes.imgURL; 
       picLoader.loadClip(imgPath,img_mc); 
    } 
    var image_bitmap; 
    function getPix() { 
       image_bitmap = new flash.display.BitmapData(img_mc._width, img_mc._height); 
       image_bitmap.draw(img_mc,new Matrix()); 
       var channelBitmap = image_bitmap.clone(); 
       channel_mc.attachBitmap(channelBitmap, 1); 
    
    var sourceRect = image_bitmap.rectangle; // source rectangle area 
    var offset = new flash.geom.Point(0,0); // offset for drawing channel(s) 
       channelBitmap.copyChannel(image_bitmap, sourceRect, offset, 8, 8); 
    
    } 
    
    onMouseMove = function () { 
       picker_mc._x = _xmouse; 
       picker_mc._y = _ymouse; 
       var curr_color:Number = image_bitmap.getPixel32(img_mc._xmouse, img_mc._ymouse); 
       var preview_colortansform = new flash.geom.ColorTransform(); 
       preview_colortansform.rgb = curr_color; 
       color_mc.transform.colorTransform = preview_colortansform; 
        
       var alpha:String = (curr_color >> 24 & 0xFF).toString(16); 
       trace(">> alpha: "+alpha); 
       // ff 
       var red:String = (curr_color >> 16 & 0xFF).toString(16); 
       trace(">> red: "+red); 
       // aa 
       var green:String = (curr_color >> 8 & 0xFF).toString(16); 
       trace(">> green: "+green); 
       // cc 
       var blue:String = (curr_color & 0xFF).toString(16); 
       trace(">> blue: "+blue); 
       // ee 
       trace("0x"+alpha+red+green+blue); 
       // 0xffaaccee 
       hex_txt.text = "0x"+alpha+red+green+blue; 
       updateAfterEvent(); 
    };

  12. #12
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The reason is simple. That is the alpha value of the pixels. Changing the alpha value of an image would not change the alpha value of the pixels. You can test that with this little script:

    import flash.display.BitmapData;

    var myBitmapData:BitmapData = new BitmapData(100, 80, true, 0xFFAACCEE);

    var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
    mc.attachBitmap(myBitmapData, this.getNextHighestDepth());

    var alpha:String = (myBitmapData.getPixel32(0, 0) >> 24 & 0xFF).toString(16);
    trace(">> alpha: " + alpha);

    Change FF to 00 or 11 for example and the trace for alpha will change.
    - The right of the People to create Flash movies shall not be infringed. -

  13. #13
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    http://www.senocular.com/flash/source.php?id=0.168
    take a look on this sample~

    the photo is partially transparent, i can see that in photoshop
    if i put my code in this file, the color picker can tell the alpha changes.

    that why i still think there is a chance for getting the alpha value from external .png~

    jsut don'y know how to achieve it T_T

  14. #14
    Junior Member
    Join Date
    Jun 2005
    Posts
    29
    i should better clarify the goal.

    i want to hide the answer by alpha channel mask (say: black:true, white:false)
    i add an alpha channel mask on picture by photoshop.
    there would be no transparent you can see but the mask in alpha channel.


    so if i use getPixel32() in flash, is it possible to tell the alpha channel mask value?
    alpha: FF/ 00

  15. #15
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    As I stated above the getPixel32() method recognizes the alpha value of individual pixels of a Bitmap. Whatever you do, masking etc, you need to convert the new image to a Bitmap and then you can get the true values. So let's say you have an image, then you change the alpha value of that image, you need to draw it and create a new bitmap, then the getPixel32() method will return the new alpha value.
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center