请问JAVA如何取出一副图每个像素的颜色,在改变颜色,最后写入到图片?
答案:2 悬赏:60 手机版
解决时间 2021-03-07 10:57
- 提问者网友:世勋超人
- 2021-03-07 02:18
请问JAVA如何取出一副图每个像素的颜色,然后在去改变颜色,然后再写入到图片?我是菜中菜鸟。。。请指教!!!!
最佳答案
- 五星知识达人网友:大漠
- 2021-03-07 02:51
//读取颜色..以前写的 写入自己搞定
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);
}
}
全部回答
- 1楼网友:酒醒三更
- 2021-03-07 04:08
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();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯