请问JAVA如何取出一副图每个像素的颜色,在改变颜色,最后写入到图片?
请问JAVA如何取出一副图每个像素的颜色,然后在去改变颜色,然后再写入到图片?我是菜中菜鸟。。。请指教!!!!...
请问JAVA如何取出一副图每个像素的颜色,然后在去改变颜色,然后再写入到图片?我是菜中菜鸟。。。请指教!!!!
展开
3个回答
展开全部
//读取颜色..以前写的 写入自己搞定
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class BackroundImage extends JFrame
{
private static final long serialVersionUID = 1L;
JScrollPane scrollPane;
ImageIcon icon;
Image image;
Robot rb = null;
public BackroundImage()
{
//ken.jpg要位于此java文件项目目录下,我的ide :eclipse
icon = new ImageIcon("ken.jpg");
JPanel panel = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(800, 600) );
panel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
//转化成#ffffff格式
Color c = rb.getPixelColor(x, y);
String r = Integer.toHexString(c.getRed());
String g = Integer.toHexString(c.getGreen());
String b = Integer.toHexString(c.getBlue());
if(r.length() == 1) r += "0";
if(g.length() == 1) g += "0";
if(b.length() == 1) b += "0";
//color就是获得点颜色的字符串表示
String color = "#" + r + g +b;
System.out.println(color);
}
});
scrollPane = new JScrollPane( panel );
getContentPane().add( scrollPane );
init();
}
public void init() {
try {
rb = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void main(String [] args)
{
BackroundImage frame = new BackroundImage();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class BackroundImage extends JFrame
{
private static final long serialVersionUID = 1L;
JScrollPane scrollPane;
ImageIcon icon;
Image image;
Robot rb = null;
public BackroundImage()
{
//ken.jpg要位于此java文件项目目录下,我的ide :eclipse
icon = new ImageIcon("ken.jpg");
JPanel panel = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
panel.setPreferredSize( new Dimension(800, 600) );
panel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
//转化成#ffffff格式
Color c = rb.getPixelColor(x, y);
String r = Integer.toHexString(c.getRed());
String g = Integer.toHexString(c.getGreen());
String b = Integer.toHexString(c.getBlue());
if(r.length() == 1) r += "0";
if(g.length() == 1) g += "0";
if(b.length() == 1) b += "0";
//color就是获得点颜色的字符串表示
String color = "#" + r + g +b;
System.out.println(color);
}
});
scrollPane = new JScrollPane( panel );
getContentPane().add( scrollPane );
init();
}
public void init() {
try {
rb = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void main(String [] args)
{
BackroundImage frame = new BackroundImage();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
展开全部
BufferedImage buimage=ImageIO.read(new File("c:\\a.jpg"));//换上你的文件名
int w=buima.getWidth();
int h=buima.getHeight();
int RGBS[]=new int[w*h];
buimage.getRGB(0,0,w,h, RGBS,0,w);
byte b=0;
for(int i=0;i<w*h;i++){//循环体换上你要的操作,目前是灰度渐变
b=(byte)i%w%255;
RGBS[i]=b<<16+b<<8+b;
}
buimage.setRGB(0,0,w,h,RGBS,0,w);
最后要把buimage强制转化为java.awt.Image就可以使用了。
int w=buima.getWidth();
int h=buima.getHeight();
int RGBS[]=new int[w*h];
buimage.getRGB(0,0,w,h, RGBS,0,w);
byte b=0;
for(int i=0;i<w*h;i++){//循环体换上你要的操作,目前是灰度渐变
b=(byte)i%w%255;
RGBS[i]=b<<16+b<<8+b;
}
buimage.setRGB(0,0,w,h,RGBS,0,w);
最后要把buimage强制转化为java.awt.Image就可以使用了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/**
*
从图像中取出每个点的像素值,放到一个数组中
*
*
@param
buffer
存放像素值的数组
*/
private
void
pixelgrabber(int
buffer[])
{
try
{
mediatracker
tracker
=
new
mediatracker(this);
tracker.addimage(img,
0);
tracker.waitforall();
pixelgrabber
grabber
=
new
pixelgrabber(img,
0,
0,
imgwidth,
imgheight,
buffer,
0,
imgwidth);
try
{
grabber.grabpixels();
}
catch
(interruptedexception
e)
{
e.printstacktrace();
}
}
catch
(exception
e)
{
e.printstacktrace();
}
}
*
从图像中取出每个点的像素值,放到一个数组中
*
*
@param
buffer
存放像素值的数组
*/
private
void
pixelgrabber(int
buffer[])
{
try
{
mediatracker
tracker
=
new
mediatracker(this);
tracker.addimage(img,
0);
tracker.waitforall();
pixelgrabber
grabber
=
new
pixelgrabber(img,
0,
0,
imgwidth,
imgheight,
buffer,
0,
imgwidth);
try
{
grabber.grabpixels();
}
catch
(interruptedexception
e)
{
e.printstacktrace();
}
}
catch
(exception
e)
{
e.printstacktrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询