【问题标题】:how i can rotate image by array of byte in java?我如何在java中按字节数组旋转图像?
【发布时间】:2015-05-06 18:12:30
【问题描述】:

我有图像的字节数组,我想通过这个数组旋转图像, 这是我的代码:

BufferedImage img = ImageUtil.load(inputImagePath);
WritableRaster raster = img .getRaster();
DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

byte [] pixel = data.getData();

我怎么能做到这一点? , 谢谢

【问题讨论】:

  • 为什么需要旋转字节数组?你能用This吗?
  • @JAtkin ,我的目标是通过仅使用数组创建旋转图像的新算法,如何至少获取像素位置?
  • 如果你想创建一个新算法,那么到底要问什么?我们为您创建它?如果你想实现一些算法,那么试着去做,如果你遇到问题,再问更具体的问题。
  • 通过字节数组处理像素也是无效的,更多信息请参见this答案。
  • @NikolayKondratyev 我的问题是我没有对像素的任何引用,但现在我通过使用这个: int w = img.getWidth(null); int h = img.getHeight(null); int[] rgbs = new int[w*h]; img.getRGB(0, 0, w, h, rgbs, 0, w); _ 我创建了自己的旋转和翻转图像算法 - 感谢您的评论

标签: java arrays byte raster image-rotation


【解决方案1】:

经过一番努力,我想出了这个:

public class ImageRotation {
    public static void main(String[] args) throws IOException {

        BufferedImage img = ImageIO.read(
                ImageRotation.class
                        .getResourceAsStream("Capture.PNG"));

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(
                new JLabel("Original", new ImageIcon(img), JLabel.CENTER),
                BorderLayout.WEST);

        pane.add(
                new JLabel("Rotated", new ImageIcon(rotateClockwise(img)), JLabel.CENTER),
                BorderLayout.EAST);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(pane);
        frame.setVisible(true);
        frame.pack();
    }

    static BufferedImage rotateClockwise(BufferedImage img) {
        int[] origPix = getIntBuff(img);

        int newWidth = img.getHeight();
        int newHeight = img.getWidth();
        int[] buff = new int[newWidth * newHeight];

        // formula for determining pixel mapping
        // (sizeOf(old y) - 1) - old y -> new x
        // old x -> new y

        for (int x = 0; x < img.getWidth(); x++)
            for (int y = 0; y < img.getHeight(); y++) {

                int pix = origPix[x + (y * img.getWidth())];
                int newX = img.getHeight() - 1 - y, newY = x;

                buff[newX + (newWidth * newY)] = pix;
            }
        // we have now rotated the array clockwise, time to place the buffer in an image


        int type = BufferedImage.TYPE_INT_ARGB;
        BufferedImage ret = new BufferedImage(newWidth, newHeight, type);
        WritableRaster wr = ret.getRaster();
        wr.setDataElements(0, 0, newWidth, newHeight, buff);
        return ret;
    }

    // variation of convertTo2DWithoutUsingGetRGB http://stackoverflow.com/a/9470843/4683264
    private static int[] getIntBuff(BufferedImage image) {

        final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        final int width = image.getWidth();
        final int height = image.getHeight();
        final boolean hasAlphaChannel = image.getAlphaRaster() != null;

        int[] result = new int[height * width];

        final int pixelLength = hasAlphaChannel ? 4 : 3;
        for (int pixel = 0, resInd = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            if (hasAlphaChannel)
                argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
            else
                argb += -16777216; // 255 alpha

            argb += ((int) pixels[pixel + 1] & 0xff); // blue
            argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
            argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
            result[resInd++] = argb;
        }

        return result;
    }

}

结果:

现在它只顺时针旋转图像,但是一旦你找到从旧图像到新图像的像素映射,比如逆时针,你只需要在 rotateClockwise 方法中的嵌套 for 循环中进行更改即可:

int newX = y, newY = img.getWidth() - 1 - x;

【讨论】:

  • 谢谢你,我没有使用你的代码,但我使用这个数组来旋转图像并翻转它,它工作得很好:int w = img.getWidth(null); int h = img.getHeight(null); int[] rgbs = new int[w*h]; img.getRGB(0, 0, w, h, rgbs, 0, w);
  • @safana 如果此答案对您有帮助,请选择“最佳答案”
  • 这个帖子的投票计数下的小复选标记。 stackoverflow.com/help/accepted-answer
猜你喜欢
  • 2014-10-23
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多