2010-06-10 145 views
1

如何將彩色圖像轉換爲單色as3?將彩色圖像轉換爲單色as3

+0

暫且忽略AS3,這是什麼意思?具有250,000像素的圖像可能很好地使用100,000種不同的顏色。 – MSalters 2010-06-10 07:48:26

+0

http://stackoverflow.com/questions/1098890/as3how-to-change-a-colored-bitmaps-bitmapdata-to-black-and-white – phwd 2010-06-29 12:57:01

回答

1

,我認爲那是你必須的意思是像下面的例子:Multi color image 在這個例子中,我已經採取了圖像,並使其灰階(upperleft)的右上角是相同的灰階圖像,然後用紅色疊加層將混合模式設置爲變暗。薩默與藍色和黃色。要在閃光燈中製作灰度圖像,必須爲紅色藍色和綠色通道設置相同的值。您可以在閃光燈中輕鬆完成此操作。首先,您必須擁有一個包含圖片的bitmapData對象。然後製作灰度圖,最後製作覆蓋圖並將它們混合在一起。

要得到的BitmapData圖像可以關注@adobe reference

我打了一個比方類(從示例@土坯工作)的例子正是我說這不上面:

package { 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.display.Loader; 
    import flash.display.Sprite; 
    import flash.display.BitmapDataChannel; 
    import flash.display.BlendMode; 

    import flash.events.*; 

    import flash.geom.Point; 
    import flash.geom.Rectangle; 

    import flash.net.URLRequest; 

    [SWF(width='900', height='481', backgroundColor='#FFFFFF', frameRate='30')] 
    public class BitmapExample extends Sprite { 
     private var url:String = "test.jpg"; 
     private var fillColor:uint = 0xFF0000; //0xFF0000 = Red 

     public function BitmapExample() { 
      configureAssets(); 
     } 

     private function configureAssets():void { 
      var loader:Loader = new Loader(); 
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); 
      loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 

      var request:URLRequest = new URLRequest(url); 
      loader.load(request); 
     } 

     private function completeHandler(event:Event):void { 
      var loader:Loader = Loader(event.target.loader); 
      var image:Bitmap = Bitmap(loader.content); 
      var bitmapData:BitmapData = image.bitmapData.clone(); 

      //Now we have the bitmapData we can make it grayscale. 
      //First lock the data so it shows no changes while we are doing the changes. 
      bitmapData.lock(); 
      //We now copy the red channel to the blue and the green channel. 
      bitmapData.copyChannel(bitmapData,bitmapData.rect,new Point(),BitmapDataChannel.RED,BitmapDataChannel.BLUE); 
      bitmapData.copyChannel(bitmapData,bitmapData.rect,new Point(),BitmapDataChannel.RED,BitmapDataChannel.GREEN); 
      //After the change we can unlock the bitmapData. 
      bitmapData.unlock(); 


      //Create the overlay with the color set in the private var fillColor 
      var overlay:Bitmap = this.makeOverlayBitMap(bitmapData.width, bitmapData.height, fillColor); 
      //Set the blendMode to darken so it will will be just like the picture in the post. 
      overlay.blendMode = BlendMode.DARKEN; 

      //Add original to the stage 
      image.x = 0; 
      image.y = 0; 
      addChild(image); 

      //Add a copy next to it with the grayscale data 
      var image2:Bitmap = new Bitmap(bitmapData); 
      image2.x = image.width; 
      image2.y = 0; 
      addChild(image2); 

      //Add the overlay to the stage at position of the grayscale 
      overlay.x = image2.x; 
      overlay.y = image2.y; 
      addChild(overlay); 

     } 

     private function makeOverlayBitMap(theWidth:int, theHeight:int, theColor:uint):Bitmap{ 
      var bitmapData:BitmapData = new BitmapData(theWidth,theHeight,false, theColor); 
      var returnBit:Bitmap = new Bitmap(bitmapData); 
      return returnBit; 
     } 

     private function ioErrorHandler(event:IOErrorEvent):void { 
      trace("Unable to load image: " + url); 
     } 
    } 
} 

我希望其中的代碼和評論能夠說話。如果你有問題請問他們。

相關問題