當前位置:首頁 » 編程語言 » java圖像

java圖像

發布時間: 2022-01-10 05:32:57

java添加圖片

JAVA是非常靈活的語言,你自己稍動一下手指就能編個控制項的。
class ImageBox extends JLabel ...

⑵ Java的圖像演算法

//沒聽懂你的需求,能更清楚一些嗎?

我以前的一個程序使用BufferedImage讀取圖像像素點的RGB值,例子在下面。

你從例子中了解一下用一個整數表示RGB顏色的方法。然後用setRGB方法滿足你的要求。

方法:
java.awt.image.BufferedImage

public int getRGB(int x,int y)
返回默認 RGB 顏色模型 (TYPE_INT_ARGB) 和默認 sRGB 顏色空間中的整數像素。如果此默認模型與該圖像的 ColorModel 不匹配,則發生顏色轉換。在使用此方法所返回的數據中,每個顏色分量只有 8 位精度。

public void setRGB(int x, int y,int rgb)
將此 BufferedImage 中的像素設置為指定的 RGB 值。假定該像素使用默認 RGB 顏色模型、TYPE_INT_ARGB 和默認 sRGB 顏色空間。對於具有 IndexColorModel 的圖像,則選擇最接近的顏色的索引。
參數:
x - 要設置的像素的 X 坐標
y - 要設置的像素的 Y 坐標
rgb - RGB 值

例子:(獲取RGB值)---了解一下用一個整數表示RGB顏色的方法
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;

public class Test{
public static void main(String args[]) {
int[] rgb = new int[3];

File file = new File("a.bmp");
BufferedImage bi=null;
try{
bi = ImageIO.read(file);
}catch(Exception e){
e.printStackTrace();
}

int width=bi.getWidth();
int height=bi.getHeight();
int minx=bi.getMinX();
int miny=bi.getMinY();
System.out.println("width="+width+",height="+height+".");
System.out.println("minx="+minx+",miniy="+miny+".");

for(int i=minx;i<width;i++){
for(int j=miny;j<height;j++){
//System.out.print(bi.getRGB(jw, ih));
int pixel=bi.getRGB(i, j);
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
System.out.println("i="+i+",j="+j+":("+rgb[0]+","+rgb[1]+","+rgb[2]+")");

}
}

}

}

⑶ JAVA 畫圖片

如果image是在同一個包下的話,可以直接在路勁上寫你的image的名字。比如包名叫com..chaojunjie 那麼下面有個Test.java,在裡面再加一張圖片test.image,那麼久直接Toolkit.getDeafaultToolkit.getImage('test.image')就可以了。當然這樣寫不是做好的。最好單獨和src同目錄下建個包叫images然後把拓片單獨放裡面。
然後獲得相對路徑Toolkit.getDeafaultToolkit.getImage(this.getClass.getFile('裡面寫你的包名+圖片名'))

⑷ java適合做圖像處理嗎

Java圖像處理技巧四則
下面代碼中用到的sourceImage是一個已經存在的Image對象

圖像剪切
對於一個已經存在的Image對象,要得到它的一個局部圖像,可以使用下面的步驟:

//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75); //四個參數分別為圖像起點坐標和寬高,即CropImageFilter(int x,int y,int width,int height),詳細情況請參考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子類中使用,可以將上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一個ImageProcer對象。
圖像縮放
對於一個已經存在的Image對象,得到它的一個縮放的Image對象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一個100X100的圖像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一個放大兩倍的圖像,這個程序一般在一個swing的組件中使用,而類Jcomponent實現了圖像觀察者介面ImageObserver,所有可以使用this。
//其它情況請參考API

灰度變換
下面的程序使用三種方法對一個彩色圖像進行灰度變換,變換的效果都不一樣。一般而言,灰度變換的演算法是將象素的三個顏色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然後將之賦值給紅綠藍,這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍三色中的最大值作為灰度值。java核心包也有一種演算法,但是沒有看源代碼,不知道具體演算法是什麼樣的,效果和上述不同。

/* GrayFilter.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class GrayFilter extends RGBImageFilter {
int modelStyle;
public GrayFilter() {
modelStyle=GrayModel.CS_MAX;
canFilterIndexColorModel=true;
}
public GrayFilter(int style) {
modelStyle=style;
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
if (modelStyle==GrayModel
else if (modelStyle==GrayModel
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
/* GrayModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class GrayModel extends ColorModel {
public static final int CS_MAX=0;
public static final int CS_FLOAT=1;
ColorModel sourceModel;
int modelStyle;

public GrayModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=0;
}

public GrayModel(ColorModel sourceModel,int style) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=style;
}

public void setGrayStyle(int style) {
modelStyle=style;
}

protected int getGrayLevel(int pixel) {
if (modelStyle==CS_MAX) {
return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
else if (modelStyle==CS_FLOAT){
return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else {
return 0;
}
}

public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}

public int getRed(int pixel) {
return getGrayLevel(pixel);
}

public int getGreen(int pixel) {
return getGrayLevel(pixel);
}

public int getBlue(int pixel) {
return getGrayLevel(pixel);
}

public int getRGB(int pixel) {
int gray=getGrayLevel(pixel);
return (getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;
}
}

如果你有自己的演算法或者想取得特殊的效果,你可以修改類GrayModel的方法getGrayLevel()。
色彩變換
根據上面的原理,我們也可以實現色彩變換,這樣的效果就很多了。下面是一個反轉變換的例子:

/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class ReverseColorModel extends ColorModel {
ColorModel sourceModel;
public ReverseColorModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}

public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}

public int getRed(int pixel) {
return ~sourceModel.getRed(pixel);
}

public int getGreen(int pixel) {
return ~sourceModel.getGreen(pixel);
}

public int getBlue(int pixel) {
return ~sourceModel.getBlue(pixel);
}

public int getRGB(int pixel) {
return (getAlpha(pixel)<<24)+(getRed(pixel)<<16)+(getGreen(pixel)<<8)+getBlue(pixel);
}
}
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
canFilterIndexColorModel=true;
}

public void setColorModel(ColorModel cm) {
substituteColorModel(cm,new ReverseColorModel(cm));
}

public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}

要想取得自己的效果,需要修改ReverseColorModel.java中的三個方法,getRed、getGreen、getBlue。
下面是上面的效果的一個總的演示程序。

/*GrayImage.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.*;
public class GrayImage extends JFrame{
Image source,gray,gray3,clip,bigimg;
BufferedImage bimg,gray2;
GrayFilter filter,filter2;
ImageIcon ii;
ImageFilter cropFilter;
int iw,ih;

public GrayImage() {
ii=new ImageIcon(\"images/11.gif\");
source=ii.getImage();
iw=source.getWidth(this);
ih=source.getHeight(this);
filter=new GrayFilter();
filter2=new GrayFilter(GrayModel.CS_FLOAT);
gray=createImage(new FilteredImageSource(source.getSource(),filter));
gray3=createImage(new FilteredImageSource(source.getSource(),filter2));
cropFilter=new CropImageFilter(5,5,iw-5,ih-5);
clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));
bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTracker mt=new MediaTracker(this);
mt.addImage(gray,0);
try {
mt.waitForAll();
} catch (Exception e) {
}
}

public void paint(Graphics g) {
Graphics2D g2=(Graphics2D)g;
bimg=new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
Graphics2D srcG = bimg.createGraphics();
RenderingHints rhs = g2.getRenderingHints();
srcG.setRenderingHints(rhs);
srcG.drawImage(source, 0, 0, null);
ColorSpace graySpace=ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op=new ColorConvertOp(graySpace,rhs);
gray2=new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
op.filter(bimg,gray2);
g2.drawImage(source,40,40,this);
g2.drawImage(gray,80,40,this);
g2.drawImage(gray2,120,40,this);
g2.drawImage(gray3,160,40,this);
g2.drawImage(clip,40,80,this);
g2.drawImage(bigimg,80,80,this);
}

public void update(Graphics g) {
paint(g);
}

public static void main(String args[]) {
GrayImage m=new GrayImage();
m.setSize(400,400);
m.setVisible(true);
}
}

⑸ 關於java圖像處理

Java圖像處理技巧四則
下面代碼中用到的sourceImage是一個已經存在的Image對象

圖像剪切
對於一個已經存在的Image對象,要得到它的一個局部圖像,可以使用下面的步驟:

//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75); //四個參數分別為圖像起點坐標和寬高,即CropImageFilter(int x,int y,int width,int height),詳細情況請參考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子類中使用,可以將上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一個ImageProcer對象。
圖像縮放
對於一個已經存在的Image對象,得到它的一個縮放的Image對象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一個100X100的圖像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一個放大兩倍的圖像,這個程序一般在一個swing的組件中使用,而類Jcomponent實現了圖像觀察者介面ImageObserver,所有可以使用this。
//其它情況請參考API

灰度變換
下面的程序使用三種方法對一個彩色圖像進行灰度變換,變換的效果都不一樣。一般而言,灰度變換的演算法是將象素的三個顏色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然後將之賦值給紅綠藍,這樣顏色取得的效果就是灰度的。另一種就是取紅綠藍三色中的最大值作為灰度值。java核心包也有一種演算法,但是沒有看源代碼,不知道具體演算法是什麼樣的,效果和上述不同。

/* GrayFilter.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class GrayFilter extends RGBImageFilter {
int modelStyle;
public GrayFilter() {
modelStyle=GrayModel.CS_MAX;
canFilterIndexColorModel=true;
}
public GrayFilter(int style) {
modelStyle=style;
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
if (modelStyle==GrayModel
else if (modelStyle==GrayModel
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
/* GrayModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class GrayModel extends ColorModel {
public static final int CS_MAX=0;
public static final int CS_FLOAT=1;
ColorModel sourceModel;
int modelStyle;

public GrayModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=0;
}

public GrayModel(ColorModel sourceModel,int style) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=style;
}

public void setGrayStyle(int style) {
modelStyle=style;
}

protected int getGrayLevel(int pixel) {
if (modelStyle==CS_MAX) {
return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
else if (modelStyle==CS_FLOAT){
return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else {
return 0;
}
}

public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}

public int getRed(int pixel) {
return getGrayLevel(pixel);
}

public int getGreen(int pixel) {
return getGrayLevel(pixel);
}

public int getBlue(int pixel) {
return getGrayLevel(pixel);
}

public int getRGB(int pixel) {
int gray=getGrayLevel(pixel);
return (getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;
}
}

如果你有自己的演算法或者想取得特殊的效果,你可以修改類GrayModel的方法getGrayLevel()。
色彩變換
根據上面的原理,我們也可以實現色彩變換,這樣的效果就很多了。下面是一個反轉變換的例子:

/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class ReverseColorModel extends ColorModel {
ColorModel sourceModel;
public ReverseColorModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}

public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}

public int getRed(int pixel) {
return ~sourceModel.getRed(pixel);
}

public int getGreen(int pixel) {
return ~sourceModel.getGreen(pixel);
}

public int getBlue(int pixel) {
return ~sourceModel.getBlue(pixel);
}

public int getRGB(int pixel) {
return (getAlpha(pixel)<<24)+(getRed(pixel)<<16)+(getGreen(pixel)<<8)+getBlue(pixel);
}
}
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
canFilterIndexColorModel=true;
}

public void setColorModel(ColorModel cm) {
substituteColorModel(cm,new ReverseColorModel(cm));
}

public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}

要想取得自己的效果,需要修改ReverseColorModel.java中的三個方法,getRed、getGreen、getBlue。
下面是上面的效果的一個總的演示程序。

/*GrayImage.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.*;
public class GrayImage extends JFrame{
Image source,gray,gray3,clip,bigimg;
BufferedImage bimg,gray2;
GrayFilter filter,filter2;
ImageIcon ii;
ImageFilter cropFilter;
int iw,ih;

public GrayImage() {
ii=new ImageIcon(\"images/11.gif\");
source=ii.getImage();
iw=source.getWidth(this);
ih=source.getHeight(this);
filter=new GrayFilter();
filter2=new GrayFilter(GrayModel.CS_FLOAT);
gray=createImage(new FilteredImageSource(source.getSource(),filter));
gray3=createImage(new FilteredImageSource(source.getSource(),filter2));
cropFilter=new CropImageFilter(5,5,iw-5,ih-5);
clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));
bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTracker mt=new MediaTracker(this);
mt.addImage(gray,0);
try {
mt.waitForAll();
} catch (Exception e) {
}

⑹ JAVA圖片輸出

等著拿分.......
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
* @author alanwei
*
*/
public class Test {

public static BufferedImage createImage(int width, int height, String s) {
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);

FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;

g2.drawString(s, (int)x, (int)baseY);

return bi;
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedImage image = createImage(100, 20, "123456789");

File file = new File("image.jpg");
if (!file.exists()) {
file.createNewFile();
}

if (image != null) {
ImageIO.write(image, "jpg", file);
}
}
}

⑺ java圖像編程

思路很簡單,設置按鈕的監聽器,你認真看一下監聽器的教程。比如你設置一個按鈕B,然後給B加上一個監聽器,當按下B的時候跳轉到另一個窗口,看下面代碼實現了一個簡單跳轉!希望能幫到你。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultiListener {

private Frame f = new Frame("測試");
private Frame f1 = new Frame("另一個窗口");
private TextArea ta = new TextArea(6 , 40);
private Button b1 = new Button("按鈕一");
private Button b2 = new Button("按鈕二");
public void init(){
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f1.pack();
f1.setVisible(true);

}
});
f.add(ta);
Panel p = new Panel();
p.add(b1);
p.add(b2);
f.add(p , BorderLayout.SOUTH);
f.pack();
f.setVisible(true);

}
}

⑻ JAVA 一個可以打開圖片的圖形界面,求代碼。

以前寫的一個比較粗糙的例子。

importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
importjava.io.*;

{
JLabeljl=newJLabel("圖片");
JMenuBarjmb=newJMenuBar();
JMenujm=newJMenu("文件");
JMenuItemjmi=newJMenuItem("選擇圖片");
JPaneljp=newJPanel(newFlowLayout(FlowLayout.CENTER));
JFileChooserchooser=newJFileChooser();
publicTupian(){
super("瀏覽圖片");
jmb.add(jm);
jm.add(jmi);
jp.add(jl);
jmi.addActionListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(newBorderLayout());
this.add(jmb,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
this.setSize(800,600);
this.setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
inti=chooser.showOpenDialog(this);
if(i==chooser.APPROVE_OPTION){
Imageimage=newImageIcon(chooser.getSelectedFile().getPath()).getImage();
image=image.getScaledInstance(400,400,Image.SCALE_DEFAULT);//調整圖像大小400,400
jl.setIcon(newImageIcon(image));
jl.setText("");
}
if(i==chooser.CANCEL_OPTION)return;
}
publicstaticvoidmain(String[]args){
newTupian();
}
}

⑼ java適合做圖像處理嗎

最近正准備造這方面的輪子,會邊做邊寫博客,剛剛搞了個圖像識別,造了些類似直線繪制啊方程計算之類的輪子,然而發現做出智能的識別很困難,於是想先造個游戲引擎輪子,練習一下圖像處理。
我這就先佔坑吧,release了第一版再來詳細回答。
先說清楚我是JVM工作者,語言是Kotlin不過做出來的引擎用Java調沒關系。看到豐愷的引擎那麼流暢,我認為Java是沒問題的,順帶還跨平台了

⑽ 怎麼用java從文件中讀取圖片和寫入圖片到文件里

樓下的答案有所欠缺,我這里有個比較完整的,也很簡潔的方法,大家參考下import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import javax.imageio.ImageIO;
import java.io.*;public class image {
public static void main(String []args)throws IOException{
Image[] array = new Image[10];
Image image = ImageIO.read(new File("c:\\supermaket1.jpg"));//這里是你要讀取的圖像文件
array[0] = image;
ImageIO.write((RenderedImage) image, "png", new File("f:\\test.txt"));//這里是你要寫入的文件,如果不存在這個文件,那麼系統會自動創建它
}

熱點內容
聚合腳本平台 發布:2024-09-20 17:51:55 瀏覽:180
訪問攔截怎麼解除安卓 發布:2024-09-20 17:28:48 瀏覽:275
蘿卜干存儲 發布:2024-09-20 17:21:37 瀏覽:715
蘋果手機如何遷移軟體到安卓手機 發布:2024-09-20 17:21:34 瀏覽:692
查看伺服器ip限制 發布:2024-09-20 16:56:27 瀏覽:389
p搜系統只緩存1頁為什麼 發布:2024-09-20 16:48:51 瀏覽:839
上網的賬號和密碼是什麼東西 發布:2024-09-20 16:31:31 瀏覽:612
安卓手機王者榮耀如何調超高視距 發布:2024-09-20 16:31:30 瀏覽:428
安卓G是什麼app 發布:2024-09-20 16:23:09 瀏覽:81
iphone怎麼壓縮文件 發布:2024-09-20 16:08:18 瀏覽:356