当前位置: 技术问答>java相关
怎样用java实现在图片上填色。急!
来源: 互联网 发布时间:2017-04-06
本文导语: 怎样用java实现在图片上填色。给出RED, Green , Blue 三种颜色的int值(0--255)。 | 假如im一定用上面的方法得到 OutputStream out=new FileOutputStream("c:\myImage.jpg"); int width=im.getWidth(); int height=im.getHei...
怎样用java实现在图片上填色。给出RED, Green , Blue 三种颜色的int值(0--255)。
|
假如im一定用上面的方法得到
OutputStream out=new FileOutputStream("c:\myImage.jpg");
int width=im.getWidth();
int height=im.getHeight();
BufferedImage myImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
im.paint(g2);
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
throw new Exception("GRAPHICS ERROR,CANNOT CREATE JPEG FORMAT");
}
OutputStream out=new FileOutputStream("c:\myImage.jpg");
int width=im.getWidth();
int height=im.getHeight();
BufferedImage myImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
im.paint(g2);
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
throw new Exception("GRAPHICS ERROR,CANNOT CREATE JPEG FORMAT");
}
|
继承java.awt.image.RGBImageFilter类
class myImage extends RGBImageFilter {
int red=0;
int green=0;
int blue=0;
public myImage(int red,int green,int blue) {
this.canFilterIndexColorModel=true;
this.red=red;
this.green=green;
this.blue=blue;
}
public int filterRGB(int x,int y,int rgb) {
DirectColorModel dcm=(DirectColorModel)ColorModel.getRGBdefault();
int alpha=dcm.getAlpha(rgb);
return alpha
class myImage extends RGBImageFilter {
int red=0;
int green=0;
int blue=0;
public myImage(int red,int green,int blue) {
this.canFilterIndexColorModel=true;
this.red=red;
this.green=green;
this.blue=blue;
}
public int filterRGB(int x,int y,int rgb) {
DirectColorModel dcm=(DirectColorModel)ColorModel.getRGBdefault();
int alpha=dcm.getAlpha(rgb);
return alpha