java设置rgb_将Color设置为int值以便在setRGB(int x,int y,int rgb)方法中使用? — Java

除了其他错误,我需要一种将我的Color grayScale转换为int的方法.插入颜色时,出现错误. setRGB方法将x,y和rgb int作为参数.如何将我的颜色更改为整数?

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import javax.swing.*;

import java.awt.image.*;

import javax.imageio.ImageIO;

public class Picture{

Container content;

BufferedImage image, image2;

public Picture(String filename) {

File f = new File(filename);

//assume file is the image file

try {

image = ImageIO.read(f);

}

catch (IOException e) {

System.out.println("Invalid image file: " + filename);

System.exit(0);

}

}

public void show() {

final int width = image.getWidth();

final int height = image.getHeight();

JFrame frame = new JFrame("Edit Picture");

//set frame title, set it visible, etc

content = frame.getContentPane();

content.setPreferredSize(new Dimension(width, height));

frame.pack();

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//add a menubar on the frame with a single option: saving the image

JMenuBar menuBar = new JMenuBar();

frame.setJMenuBar(menuBar);

JMenu fileMenu = new JMenu("File");

menuBar.add(fileMenu);

JMenuItem saveAction = new JMenuItem("Save");

fileMenu.add(saveAction);

JMenuItem grayScale = new JMenuItem("Grayscale");

fileMenu.add(grayScale);

grayScale.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

grayscale(width, height);

}

});

//add the image to the frame

ImageIcon icon = new ImageIcon(image);

frame.setContentPane(new JLabel(icon));

//paint the frame

frame.setVisible(true);

frame.repaint();

}

public void grayscale(int width, int height) {

for (int i = 0; i < width; i++) {

for (int j = 0; j < height; j++) {

Color color = new Color(image.getRGB(i, j));

int red = color.getRed();

int green = color.getGreen();

int blue = color.getBlue();

int rGray = red*(1/3);

int gGray = green*(2/3);

int bGray = blue*(1/10);

Color grayScale = new Color(rGray, gGray, bGray);

image.setRGB(i,j, grayScale);

}

}

show();

}

public static void main(String[] args) {

Picture p = new Picture(args[0]);

p.show();

}

}

解决方法:

根据垃圾桶的评论.

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import javax.swing.*;

import java.awt.image.*;

public class Picture{

Container content;

BufferedImage image;

BufferedImage image2;

public Picture(BufferedImage image) {

this.image = image;

grayscale();

}

public void show() {

JFrame frame = new JFrame("Edit Picture");

//set frame title, set it visible, etc

content = frame.getContentPane();

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//add the image to the frame

ImageIcon icon = new ImageIcon(image2);

frame.setContentPane(new JLabel(icon));

frame.pack();

//paint the frame

frame.setVisible(true);

}

public void grayscale() {

// create a grayscale image the same size

image2 = new BufferedImage(

image.getWidth(),

image.getHeight(),

BufferedImage.TYPE_BYTE_GRAY);

// convert the original colored image to grayscale

ColorConvertOp op = new ColorConvertOp(

image.getColorModel().getColorSpace(),

image2.getColorModel().getColorSpace(),null);

op.filter(image,image2);

show();

}

public static void main(String[] args) {

int size = 120;

int pad = 10;

BufferedImage bi = new BufferedImage(

size,

size,

BufferedImage.TYPE_INT_RGB);

Graphics g = bi.createGraphics();

g.setColor(Color.WHITE);

g.fillRect(0,0,size,size);

g.setColor(Color.YELLOW);

g.fillOval(pad,pad,size-(2*pad),size-(2*pad));

g.dispose();

Picture p = new Picture(bi);

p.show();

}

}

标签:java,image,image-processing,methods,bufferedimage

来源: https://codeday.me/bug/20191013/1911039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值