当前位置:首页 » 编程语言 » 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"));//这里是你要写入的文件,如果不存在这个文件,那么系统会自动创建它
}

热点内容
android弹出布局 发布:2024-09-20 19:14:29 浏览:981
预算法包括 发布:2024-09-20 18:52:07 浏览:764
什么数字后面跟着密码 发布:2024-09-20 18:52:07 浏览:878
订座源码 发布:2024-09-20 18:52:06 浏览:381
手机mud源码 发布:2024-09-20 18:51:28 浏览:940
3k我的使命脚本 发布:2024-09-20 18:11:43 浏览:690
建设银行密码怎么设置 发布:2024-09-20 18:11:04 浏览:95
聚合脚本平台 发布:2024-09-20 17:51:55 浏览:180
访问拦截怎么解除安卓 发布:2024-09-20 17:28:48 浏览:275
萝卜干存储 发布:2024-09-20 17:21:37 浏览:716