对BuffereImage的一部分进行像素化

tyg4sfes  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(319)

我需要对BuffereImage的一部分进行像素化。我试过修改代码,从如何用java像素化jpg?但是,这是为整个图像设计的,并且似乎不适用于特定的部分(我只是将黑色图像输出为黑色)

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;

预期结果:

polhcujo

polhcujo1#

很好用。我已经调整了没有包含的部分的代码。
这是工作代码。

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);
        }
    }
}

输入图像

输出图像

相关问题