程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java數字圖象處置基本應用imageio寫圖象文件示例

java數字圖象處置基本應用imageio寫圖象文件示例

編輯:關於JAVA

java數字圖象處置基本應用imageio寫圖象文件示例。本站提示廣大學習愛好者:(java數字圖象處置基本應用imageio寫圖象文件示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java數字圖象處置基本應用imageio寫圖象文件示例正文


一個BufferedImage的像素數據貯存在Raster中,ColorModel外面貯存色彩空間,類型等信息,以後Java只支撐一下三種圖象格局- JPG,PNG,GIF,若何向讓Java支撐其它格局,起首要 完成Java中的圖象讀寫接口,然後打成jar,加上啟動參數- Xbootclasspath/pnewimageformatIO.jar便可。

Java中若何讀寫一個圖象文件,應用ImageIO對象便可。讀圖象文件的代碼以下:

 
File file = new File("D:\\test\\blue_flower.jpg");
BufferedImage image = ImageIO.read(file);

寫圖象文件的代碼以下:

File outputfile = new File("saved.png");
ImageIO.write(bufferedImage, "png",outputfile);

從BufferedImage對象中讀取像素數據的代碼以下:

int type= image.getType();
if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
return (int [])image.getRaster().getDataElements(x, y, width, height, pixels );
else
return image.getRGB( x, y, width, height, pixels, 0, width );

起首獲得圖象類型,假如不是32位的INT型數據,直接讀寫RGB值便可,不然須要從Raster
對象中讀取。

往BufferedImage對象中寫入像素數據異樣遵照下面的規矩。代碼以下:

int type= image.getType();
if ( type ==BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
image.getRaster().setDataElements(x, y, width, height, pixels );
else
image.setRGB(x, y, width, height, pixels, 0, width );

讀取圖象能夠由於圖象文件比擬年夜,須要必定時光的期待才可以,Java Advance Image
Processor API供給了MediaTracker對象來跟蹤圖象的加載,同步其它操作,應用辦法以下:
MediaTracker tracker = new MediaTracker(this); //初始化對象
tracker.addImage(image_01, 1); // 參加要跟蹤的BufferedImage對象image_001
tracker.waitForID(1, 10000) // 期待10秒,讓iamge_01圖象加載
從一個32位int型數據cARGB中讀取圖象RGB色彩值的代碼以下:
1 int alpha = (cARGB >> 24)& 0xff; //通明度通道
2 int red = (cARGB >> 16) &0xff;
3 int green = (cARGB >> 8) &0xff;
4 int blue = cARGB & 0xff;
將RGB色彩值寫入成一個INT型數據cRGB的代碼以下:
cRGB = (alpha << 24) | (red<< 16) | (green << 8) | blue;
創立一個BufferedImage對象的代碼以下:
BufferedImage image = newBufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
一個完全的源代碼Demo以下:


 package com.gloomyfish.swing;

 import java.awt.BorderLayout;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
 import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class PlasmaDemo extends JComponent { 

    /**
     * 
     */ 
    private static final long serialVersionUID = -2236160343614397287L; 
    private BufferedImage image = null; 
    private int size = 256;

    public PlasmaDemo() { 
        super(); 
        this.setOpaque(false); 
    } 

    protected void paintComponent(Graphics g) { 
        Graphics2D g2 = (Graphics2D)g; 
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
        g2.drawImage(getImage(), 5, 5, image.getWidth(), image.getHeight(), null); 
    } 

    private BufferedImage getImage() { 
        if(image == null) { 
            image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); 
            int[] rgbData = new int[size*size]; 
            generateNoiseImage(rgbData); 
            setRGB(image, 0, 0, size, size, rgbData);
            File outFile = new File("plasma.jpg");
            try {
                ImageIO.write(image, "jpg", outFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
        return image; 
    } 

    public void generateNoiseImage(int[] rgbData) { 
        int index = 0; 
        int a = 255; 
        int r = 0; 
        int g = 0; 
        int b = 0; 

        for(int row=0; row<size; row++) { 
            for(int col=0; col<size; col++) { 
                // set random color value for each pixel 
                r = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); 
                g = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); 
                b = (int)(128.0 + (128.0 * Math.sin((row + col) / 8.0))); 

                rgbData[index] = ((clamp(a) & 0xff) << 24) | 
                                ((clamp(r) & 0xff) << 16)  | 
                                ((clamp(g) & 0xff) << 8)   | 
                                ((clamp(b) & 0xff)); 
                index++; 
            } 
        } 

    } 

    private int clamp(int rgb) { 
        if(rgb > 255) 
            return 255; 
        if(rgb < 0) 
            return 0; 
        return rgb; 
    }   

    public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) { 
        int type = image.getType(); 
        if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB ) 
            image.getRaster().setDataElements( x, y, width, height, pixels ); 
        else 
            image.setRGB( x, y, width, height, pixels, 0, width ); 
    } 

    public static void main(String[] args) { 
        JFrame frame = new JFrame("Noise Art Panel"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.getContentPane().setLayout(new BorderLayout()); 

        frame.getContentPane().add(new PlasmaDemo(), BorderLayout.CENTER); 
        frame.setPreferredSize(new Dimension(400 + 25,450)); 
       frame.pack(); 
       frame.setVisible(true); 
   } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved