gpt4 book ai didi

java - 对 bufferedImage 的一部分进行像素化

转载 作者:行者123 更新时间:2023-11-30 02:56:22 30 4
gpt4 key购买 nike

我需要对 bufferedImage 的一部分进行像素化。我尝试修改How can I pixelate a jpg with java?中的代码然而,这是为整个图像设计的,似乎不适用于特定部分(我只是将黑色图像输出为黑色)

        Raster src = bufferedImage.getData();
WritableRaster dest = src.createCompatibleWritableRaster();
for (int y = pixelateSection.y; y < src.getHeight(); y += PIX_SIZE) {
for (int x = pixelateSection.x; x < src.getWidth(); x += PIX_SIZE) {

double[] pixel = new double[3];
pixel = src.getPixel(x, y, pixel);

for (int yd = y; (yd < y + PIX_SIZE) && (yd < dest.getHeight()); yd++) {
for (int xd = x; (xd < x + PIX_SIZE) && (xd < dest.getWidth()); xd++) {
dest.setPixel(xd, yd, pixel);
}
}
}
}
bufferedImage.setData(dest);
return bufferedImage;

预期结果:

enter image description here

最佳答案

一切正常。我已经调整了未包含的部分的代码。

这是工作代码。

package test;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestMain {

public static void main(String[] args) {
JFrame frame = new JFrame("Testing");
frame.add(new TestPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public static class TestPanel extends JPanel {
int width;
int height;
String fileName = "guy fox.jpg";
BufferedImage image;
BufferedImage pixelatedImage;

public final static Rectangle pixelateSection = new Rectangle(200, 150, 200, 300);

public static final int PIX_SIZE = 10;


public TestPanel() {
image = getImage();
width = image.getWidth();
height = image.getHeight();

pixelatedImage = pixelateImage(image);

}

private BufferedImage pixelateImage(BufferedImage bufferedImage) {
Raster src = bufferedImage.getData();
WritableRaster dest = src.createCompatibleWritableRaster();

bufferedImage.copyData(dest);

for (int y = pixelateSection.y; y < pixelateSection.y + pixelateSection.getHeight(); y += PIX_SIZE) {
for (int x = pixelateSection.x; x < pixelateSection.x + pixelateSection.getWidth(); x += PIX_SIZE) {

double[] pixel = new double[3];
pixel = src.getPixel(x, y, pixel);

for (int yd = y; (yd < y + PIX_SIZE) && (yd < dest.getHeight()); yd++) {
for (int xd = x; (xd < x + PIX_SIZE) && (xd < dest.getWidth()); xd++) {
dest.setPixel(xd, yd, pixel);
}
}
}
}

bufferedImage.setData(dest);

return bufferedImage;
}

private BufferedImage getImage() {
BufferedImage image = null;
try {
image = ImageIO.read(new File(fileName));
} catch (Exception ex) {
ex.printStackTrace();
}

return image;
}

@Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
}

输入图片

enter image description here

输出图像

enter image description here

关于java - 对 bufferedImage 的一部分进行像素化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141959/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com