Java動態圖片處理技術詳解

來源:互聯網
上載者:User
使用Java動態處理圖片非常方便,可以用於很多應用場合,比如動態輸入股市線,縮圖,動態banner圖片、動態介面風格等等。筆者也在軟體開發過程中使用了這些技術,使得軟體的介面色彩風格可以使用者自訂任何一種色彩,大大增強了軟體介面的個人化適應能力。下面我對使用Java技術實現圖片處理的整個環節做主要說明:

1、圖片的裝載。我們要處理圖片首先要裝載圖片到Java記憶體空間的圖片對象,Java用很多類來支援圖片的裝載,比如
         a、根據圖片檔案位元組數組裝載圖片:
 public  Image  loadImage(byte[]  data){
   Image  t=Toolkit.getDefaultToolkit().createImage(data);
   trackImage(t,"裝載位元組數組圖片");
   return  t;
 }
         b、根據URL來裝載圖片:
 public  Image  loadImage(String  urlpath){
   Image  t=null;
   if(a  instanceof  Applet){
     try{
       URL  u=getResourceURL(urlpath);
       t=getApplet().getImage(u);
       trackImage(t,urlpath);//檢測圖片裝載過程
     }catch(Exception  e){
       notifyListener(MediaLoadEvent.MEDIA_LOAD_FAILED,false,e.getMessage());//裝載失敗
     }
   }else{
     t=Toolkit.getDefaultToolkit().getImage(urlpath);
     trackImage(t,urlpath);//檢測圖片裝載過程
   }
   return  t;
 }
 public  Image  loadImage(String  urlpath,boolean  isurl){
   Image  t=null;
   if(isurl){
     try{
       URL  u=getResourceURL(urlpath);
       t=Toolkit.getDefaultToolkit().getImage(u);
       trackImage(t,urlpath);
     }catch(Exception  e){
       notifyListener(MediaLoadEvent.MEDIA_LOAD_FAILED,false,e.getMessage());//裝載失敗
     }      
   }else{
     t=Toolkit.getDefaultToolkit().getImage(urlpath);
     trackImage(t,urlpath);//檢測圖片裝載過程
   }
   return  t;
 }
2、圖片裝載完成後,我們可以對圖片進行處理,改變色度、亮度、對比,翻轉、鏡像等等處理操作。在這裡我只詳細講一下最底層的象素變換調整技術,其他很多方法大都是根據這個來封裝的。
 a、根據制定顏色值和一定範圍色差講圖片做透明處理:
 public  Image  transpanent(Image  repImage,Color  oldColor,int  errorRange){
   int  w=repImage.getWidth(a);
   int  h=repImage.getHeight(a);
   int[]  imgdata=parImage(repImage,w,h);
   int  rr,gg,bb;
   rr=oldColor.getRed();
   gg=oldColor.getGreen();
   bb=oldColor.getBlue();
   int  offr,offg,offb;
   for(int  i=0;i<h;i++){//漸進式掃描
     for(int  j=0;j<w;j++){//逐列掃描
       int  which=i*w+j;
       int  r=((imgdata[which]&0xff0000>>>16));
       int  g=((imgdata[which]&0xff00>>>8));
       int  b=(imgdata[which]&0xff);
       offr=Math.abs(r-rr);
       offg=Math.abs(g-gg);
       offb=Math.abs(b-bb);
       if(offr+offg+offb<errorRange)imgdata[i*w+j]=16777215;
     }
   }
   Image  t=Toolkit.getDefaultToolkit().createImage(new  MemoryImageSource(w,h,imgdata,0,w));
   trackImage(t,"裝載透明處理圖片");
   return  t;
 }
 b、對圖片做剪下處理,這裡使用了圖片處理過濾類CropImageFilter,類似的過濾器很多,分別實現不同要求的圖片處理變換。
 public  Image  crop(int  x,int  y,int  w,int  h  ,Image  img){
   ImageFilter  filter=new  CropImageFilter(x,y,w,h);
   ImageProducer  producer=new  FilteredImageSource(img.getSource(),filter);
   Image  t=a.createImage(producer);
   trackImage(t,"裝載剪下圖片");
   return  t;
 }
3、圖片複合和繪製圖片:
 使用Java的繪圖類,可對多個圖片進行疊加,並可以自訂繪製各種圖形圖象,比如實現圖片浮水印、驗證碼圖片,疊加banner圖片等。

  public  static  void  createImagePNG(String  signcode,OutputStream  out){
   BufferedImage  offimg=null;
   Graphics  offgra=null;
   while(offimg==null||offgra==null){
     if(offimg==null)offimg=new  BufferedImage(50,18,BufferedImage.TYPE_INT_RGB);
     if(offgra==null&&offimg!=null)offgra=offimg.getGraphics();
   }
   offgra.setColor(new  Color(235,235,235));
   offgra.fillRect(0,0,50,18);
   /*for(int  i=0;i<12;i++){//產生混淆背景,也可用混淆背景圖片
     offgra.setColor(Color.decode("#"+color[SystemTool.getRandom(0,color.length-1)]));
     offgra.fillOval(SystemTool.getRandom(0,48),SystemTool.getRandom(0,18),1,1);//10,4);
   }*/
   offgra.setColor(Color.red);
   //offgra.setColor(Color.decode("#"+color[SystemTool.getRandom(0,color.length-1)]));
   try{
     Font  font  =  Font.createFont(Font.TRUETYPE_FONT,com.sunstudio.util.ClassLoaderEx.getResourceInputStream(SystemConfig.getParameter("validate_font")));//  FileInputStream("c:\\\\comicbd.ttf"));
     offgra.setFont(font.deriveFont(Font.BOLD,18F));
   }catch(Exception  e){}
   offgra.drawString(signcode,1,14);
   PngEncoder  png=new  PngEncoder(Toolkit.getDefaultToolkit().createImage(offimg.getSource()),PngEncoder.ENCODE_ALPHA,PngEncoder.FILTER_LAST,3);
   try{out.write(png.pngEncode());}catch(Exception  e){}
   offgra.dispose();
 }

4。圖片的編碼輸出:
           Java的圖片對象是用於圖片裝載和處理的資料統一對象,當需要輸出到輸出裝置時需要以指定圖片檔案格式來編碼儲存,常見的圖片編碼器有jpg、bmp、gif89、gif87、png等等。筆者收集了很多圖片格式編碼器,如有需要可以和我聯絡,linlichao@163.com。
           對於輸出哪種格式的圖片只是編碼器不同,過程完全相同,編碼器就是按照不同的檔案格式的要求對圖片對象進行編碼輸出,這些都是國際標準,我們只需要調用就好了。
 PngEncoder  png=new  PngEncoder(Toolkit.getDefaultToolkit().createImage(offimg.getSource()),PngEncoder.ENCODE_ALPHA,PngEncoder.FILTER_LAST,3);
 try{out.write(png.pngEncode());}catch(Exception  e){}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.