JAVA 如何将颜色从一个 bufferedImage 复制到另一个
JAVA How to copy colors from one bufferedImage to another
我正在尝试将颜色从一个 BufferedImage 复制到另一个 bufferedImage,下面是我的代码。
我知道我可以使用 graphics.drawImage,但我需要更改某些颜色,这就是为什么我要逐像素复制颜色,而不是仅仅在另一个 BufferedImage 上绘制图像
虽然它不起作用。
"t.setRGB" 行似乎对 BufferedImage "t" 没有任何影响
保存图像 "t" 后,我得到一张空白图像。
我做错了什么?
还有一个问题。如何修改 "myColor" 方法以同时使用 "alpha" 值?
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class imgSt{
public static int rgb;
public static int myColor( int r, int g, int b){
rgb= (65536 * r) + (256 * g) + (b);
return rgb;
}
public static void main( String args[] ){
try {
BufferedImage img = ImageIO.read(new File( "8.jpg"));
BufferedImage t= new BufferedImage( img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB );
int clear=0x000000FF;
int color, alpha, r, g, b;
for(int i=0; i<img.getWidth(); ++i){
for( int j=0; j<img.getHeight(); ++j ){
color = img.getRGB(i,j);
alpha = (color>>24) & 0xff;
r = (color & 0x00ff0000) >> 16;
g = (color & 0x0000ff00) >> 8;
b = color & 0x000000ff;
t.setRGB( i,j, myColor( r, g, b ) );
}
} //for
ImageIO.write( t, "jpg", new File(" sT.jpg") );
} catch (IOException e){ e.printStackTrace(); }
}//main
}//class
您可以使用栅格来完成:
BufferedImage imsrc = ... // The source image, RGBA
BufferedImage imres = ... // The resulting image, RGB or BGR
WritableRaster wrsrc = imsrc.getRaster() ;
WritableRaster wrres = imres.getRaster() ;
for (int y=0 ; y < image.getHeight() ; y++)
for (int x=0 ; x < image.getWidth() ; x++)
{
wrres.setSample(x, y, 0, wrsrc.getSample(x, y, 0)) ;
wrres.setSample(x, y, 1, wrsrc.getSample(x, y, 1)) ;
wrres.setSample(x, y, 2, wrsrc.getSample(x, y, 2)) ;
}
使用光栅时,您无需管理图像编码,通道按红绿蓝 Alpha 排序。所以我只是浏览整个图像,然后将 RGB 像素复制到结果中。
虽然像其他答案中建议的那样使用栅格通常比使用 getRGB()/setRGB()
更快,但您的方法从根本上没有错。
问题是 getRGB()/setRGB()
方法适用于 ARGB 值,而不仅仅是 RGB。因此,当您的 myColor()
方法将 alpha 分量保留为 0 时,这基本上意味着颜色将是 100% 透明的。这就是您得到空白图像的原因。您可能需要 100% 不透明像素。
这是你的方法的一个固定版本(我通常认为坚持位移更清晰,因此转换 to/from 打包表示是相似的)以打包 ARGB 格式创建不透明颜色:
public static int myColor(int r, int g, int b) {
int argb = 0xFF << 24 | r << 16 | g << 8 | b;
return argb;
}
为了可读性,我还稍微更改了解包代码,尽管这不是绝对必要的:
int color = img.getRGB(i,j);
int alpha = (color >>> 24) & 0xff;
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff ;
int b = color & 0xff;
我正在尝试将颜色从一个 BufferedImage 复制到另一个 bufferedImage,下面是我的代码。 我知道我可以使用 graphics.drawImage,但我需要更改某些颜色,这就是为什么我要逐像素复制颜色,而不是仅仅在另一个 BufferedImage 上绘制图像 虽然它不起作用。 "t.setRGB" 行似乎对 BufferedImage "t" 没有任何影响 保存图像 "t" 后,我得到一张空白图像。 我做错了什么?
还有一个问题。如何修改 "myColor" 方法以同时使用 "alpha" 值?
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class imgSt{
public static int rgb;
public static int myColor( int r, int g, int b){
rgb= (65536 * r) + (256 * g) + (b);
return rgb;
}
public static void main( String args[] ){
try {
BufferedImage img = ImageIO.read(new File( "8.jpg"));
BufferedImage t= new BufferedImage( img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB );
int clear=0x000000FF;
int color, alpha, r, g, b;
for(int i=0; i<img.getWidth(); ++i){
for( int j=0; j<img.getHeight(); ++j ){
color = img.getRGB(i,j);
alpha = (color>>24) & 0xff;
r = (color & 0x00ff0000) >> 16;
g = (color & 0x0000ff00) >> 8;
b = color & 0x000000ff;
t.setRGB( i,j, myColor( r, g, b ) );
}
} //for
ImageIO.write( t, "jpg", new File(" sT.jpg") );
} catch (IOException e){ e.printStackTrace(); }
}//main
}//class
您可以使用栅格来完成:
BufferedImage imsrc = ... // The source image, RGBA
BufferedImage imres = ... // The resulting image, RGB or BGR
WritableRaster wrsrc = imsrc.getRaster() ;
WritableRaster wrres = imres.getRaster() ;
for (int y=0 ; y < image.getHeight() ; y++)
for (int x=0 ; x < image.getWidth() ; x++)
{
wrres.setSample(x, y, 0, wrsrc.getSample(x, y, 0)) ;
wrres.setSample(x, y, 1, wrsrc.getSample(x, y, 1)) ;
wrres.setSample(x, y, 2, wrsrc.getSample(x, y, 2)) ;
}
使用光栅时,您无需管理图像编码,通道按红绿蓝 Alpha 排序。所以我只是浏览整个图像,然后将 RGB 像素复制到结果中。
虽然像其他答案中建议的那样使用栅格通常比使用 getRGB()/setRGB()
更快,但您的方法从根本上没有错。
问题是 getRGB()/setRGB()
方法适用于 ARGB 值,而不仅仅是 RGB。因此,当您的 myColor()
方法将 alpha 分量保留为 0 时,这基本上意味着颜色将是 100% 透明的。这就是您得到空白图像的原因。您可能需要 100% 不透明像素。
这是你的方法的一个固定版本(我通常认为坚持位移更清晰,因此转换 to/from 打包表示是相似的)以打包 ARGB 格式创建不透明颜色:
public static int myColor(int r, int g, int b) {
int argb = 0xFF << 24 | r << 16 | g << 8 | b;
return argb;
}
为了可读性,我还稍微更改了解包代码,尽管这不是绝对必要的:
int color = img.getRGB(i,j);
int alpha = (color >>> 24) & 0xff;
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff ;
int b = color & 0xff;