setrgb java_java-在BufferedImage上执行setRGB会将像素更改为...

**重要更新,请参见下文! **

我正在创建一个程序,当该像素满足Java中的一组条件时,它将BufferedImage的像素更改为某种颜色.但是,当我将图像写入磁盘时,应该着色的像素改为黑色.

首先,我使用RGB代码定义颜色:

Color purple = new Color(82, 0, 99);

int PURPLE = purple.getRGB();

然后,我读取了要从文件更改为“空白”的BufferedImage的图像:

BufferedImage blank = ImageIO.read(new File("some path"));

现在,循环遍历像素,当位置(x,y)处的像素与条件匹配时,将其颜色更改为紫色:

blank.setRGB(x, y, PURPLE);

现在,将“空白”写入磁盘.

File output = new File("some other path");

ImageIO.write(blankIn, "png", output); // try-catch blocks intentionally left out

生成的文件应为“空白”,并带有一些紫色像素,但所讨论的像素为黑色.我知道一个事实,问题是setRGB而不是任何导入或导出函数,因为“空白”本身是彩色图像,并因此被写入文件.我四处阅读,看到很多文章建议我使用Graphics2D并避免使用setRGB,但没有讨论逐像素颜色更改.

我还尝试了直接位操作,如下所示:

blank.setRGB(x, y, ((82 << 16) + (0 << 8) + 99));

我可能做错了,但是如果输入正确,没关系,因为在执行此操作时像素已设置为透明(无论数字怎么说,至少可以说这很奇怪) ).

**当我尝试这样做时:

blank.setRGB(x, y, Color.RED.getRGB());

我的输出文件是灰度的,因此实际上意味着setRGB是以灰度方式修改我的图片的.我认为这实际上是一个相当简单的问题,但是解决方案使我难以理解.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用包后调用包就好了 package image; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) { long start = System.currentTimeMillis(); try { ImageUtil.resizeFix(new File("D:\\qqͼƬ\\1111.jpg"), new File("D:\\qqͼƬ\\99999.jpg"), 100, 100); long end = System.currentTimeMillis(); System.out.println("success:" + (end - start)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } jar包的源码 package image; /** * 图片缩小算法,方形区域抽样 */ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; public class ImageUtil { private int width; private int height; private int zoomWidth; private int zoomHeight; private File destFile; private BufferedImage srcBufferImage; public static void resizeFix(File srcFile, File destFile, int width, int height) throws IOException { new ImageUtil(srcFile, destFile, width, height); } public static void resizeFix(BufferedImage bufImg, File destFile, int width, int height) throws IOException { new ImageUtil(bufImg, destFile, width, height); } protected ImageUtil(File srcFile, File destFile, int zoomWidth, int zoomHeight) throws IOException { this.destFile = destFile; this.zoomWidth = zoomWidth; this.zoomHeight = zoomHeight; this.srcBufferImage = javax.imageio.ImageIO.read(srcFile); this.width = this.srcBufferImage.getWidth(); this.height = this.srcBufferImage.getHeight(); if (width <= zoomWidth && height <= zoomHeight) { FileUtils.copyFile(srcFile, destFile); } else { resizeFix(); } } protected ImageUtil(BufferedImage srcBufferImage, File destFile, int zoomWidth, int zoomHeight) throws IOException { this.destFile = destFile; this.zoomWidth = zoomWidth; this.zoomHeight = zoomHeight; this.srcBufferImage = srcBufferImage; this.width = this.srcBufferImage.getWidth(); this.height = this.srcBufferImage.getHeight(); resizeFix(); } /** * 压缩图片 * * @throws IOException */ protected void resizeFix() throws IOException { if (width <= zoomWidth && height <= zoomHeight) { resize(width, height); } else if ((float) width / height > (float) zoomWidth / zoomHeight) { resize(zoomWidth, Math.round((float) zoomWidth * height / width)); } else { resize(Math.round((float) zoomHeight * width / height), zoomHeight); } } private void resize(int w, int h) throws IOException { BufferedImage imgBuf = scaleImage(w, h); File parent = destFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } ImageIO.write(imgBuf, "jpeg", destFile); } private BufferedImage scaleImage(int outWidth, int outHeight) { int[] rgbArray = srcBufferImage.getRGB(0, 0, width, height, null, 0, width); BufferedImage pbFinalOut = new BufferedImage(outWidth, outHeight, BufferedImage.TYPE_INT_RGB); double hScale = ((double) width) / ((double) outWidth);// 宽缩小的倍数 double vScale = ((double) height) / ((double) outHeight);// 高缩小的倍数 int winX0, winY0, winX1, winY1; int valueRGB = 0; long R, G, B; int x, y, i, j; int n; for (y = 0; y < outHeight; y++) { winY0 = (int) (y * vScale + 0.5);// 得到原图高的Y坐标 if (winY0 < 0) { winY0 = 0; } winY1 = (int) (winY0 + vScale + 0.5); if (winY1 > height) { winY1 = height; } for (x = 0; x < outWidth; x++) { winX0 = (int) (x * hScale + 0.5); if (winX0 < 0) { winX0 = 0; } winX1 = (int) (winX0 + hScale + 0.5); if (winX1 > width) { winX1 = width; } R = 0; G = 0; B = 0; for (i = winX0; i < winX1; i++) { for (j = winY0; j < winY1; j++) { valueRGB = rgbArray[width * j + i]; R += getRedValue(valueRGB); G += getGreenValue(valueRGB); B += getBlueValue(valueRGB); } } n = (winX1 - winX0) * (winY1 - winY0); R = (int) (((double) R) / n + 0.5); G = (int) (((double) G) / n + 0.5); B = (int) (((double) B) / n + 0.5); valueRGB = comRGB(clip((int) R), clip((int) G), clip((int) B)); pbFinalOut.setRGB(x, y, valueRGB); } } return pbFinalOut; } private int clip(int x) { if (x < 0) return 0; if (x > 255) return 255; return x; } private int getRedValue(int rgbValue) { int temp = rgbValue & 0x00ff0000; return temp >> 16; } private int getGreenValue(int rgbValue) { int temp = rgbValue & 0x0000ff00; return temp >> 8; } private int getBlueValue(int rgbValue) { return rgbValue & 0x000000ff; } private int comRGB(int redValue, int greenValue, int blueValue) { return (redValue << 16) + (greenValue << 8) + blueValue; } } 所需要的驱动包 commons-io-1.3.2.jar commons-io-1.3.2.jar + 上面的代码 = 你下载的jar

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值