简体   繁体   English

从Java Image检索像素数据

[英]Retrieving pixel data from Java Image

Recently I have been attempting to scale pixel arrays (int[]) in Java. 最近,我一直在尝试缩放Java中的像素数组(int [])。 I used .setRGB() to add all my pixel data into the BufferedImage. 我使用.setRGB()将所有像素数据添加到BufferedImage中。 BufferedImage then offers a function called .getScaledInstance(). 然后,BufferedImage提供了一个名为.getScaledInstance()的函数。 This should work great for my purposes, but I ran into a problem. 这对我来说应该很好用,但是我遇到了一个问题。 .getScaledInstance() returns a Image, not a BufferedImage. .getScaledInstance()返回一个Image,而不是BufferedImage。 With an Image object, I cannot use .getRGB() to add all the pixel data (in int[] form) from the scaled Image back into an array. 对于Image对象,我无法使用.getRGB()将缩放后的Image中的所有像素数据(以int []形式)加回到数组中。 Is there a way to get raw pixel data from an Image file? 有没有办法从图像文件中获取原始像素数据? Am I missing something? 我想念什么吗? I looked at other questions and did a bit of googling, and they only seemed to be wanting to get picture data in a different form of array (int[][]) or in bytes. 我查看了其他问题,并进行了一些谷歌搜索,他们似乎只是想以不同形式的数组(int [] [])或以字节为单位获取图片数据。 Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。 Also, Sprite is a class I made that is being used. 另外,Sprite是我正在使用的一类。 Here is my code: 这是我的代码:

public Sprite scaleSprite(Sprite s, int newWidth, int newHeight){

    BufferedImage image = new BufferedImage(s.getWidth(), s.getHeight(),  BufferedImage.TYPE_INT_RGB);
    for(int y = 0; y < s.getHeight(); y++){
        for(int x = 0; x < s.getWidth(); x++){
            image.setRGB(x, y, s.getPixel(x, y));

        }
    }
    Image newImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
    Sprite newS = new Sprite(newWidth, newHeight);
    int[] pixels = new int[newWidth * newHeight];
    newImage.getRGB(0, 0, newWidth, newHeight, pixels, 0, newWidth); //This is where I am running into problems. newImage is an Image and I cannot retrieve the raw pixel data from it.

    newS.setPixels(pixels);
    return newS;
}

To be clear, getScaledInstance() is a method of Image , not BufferedImage . 需要明确的是, getScaledInstance()Image的方法,而不是BufferedImage You don't generally want to revert to working directly with the Image superclass once you're working with BufferedImage ; 通常,在使用BufferedImage ,您通常不想恢复直接使用Image超类; Image is really not easy to work with. Image确实不容易使用。

Please see if this will help: How to scale a BufferedImage 请查看这是否有帮助: 如何缩放BufferedImage

Or from Scaling a BufferedImage , where they yield the following example: 或通过缩放BufferedImage ,它们产生以下示例:

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200,
        BufferedImage.TYPE_BYTE_INDEXED);

    AffineTransform tx = new AffineTransform();
    tx.scale(1, 2);

    AffineTransformOp op = new AffineTransformOp(tx,
        AffineTransformOp.TYPE_BILINEAR);
    bufferedImage = op.filter(bufferedImage, null);
  }

This will give you the ability to scale entirely at the level of BufferedImage . 这将使您能够完全在BufferedImage级别缩放。 From there you can apply whatever sprite specific or array data algorithm you wish. 从那里,您可以应用所需的任何特定于精灵的算法或数组数据算法。

You can draw the resulting Image onto a BufferedImage like this: 您可以像这样将生成的Image绘制到BufferedImage

Image newImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage buffImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) buffImg.getGraphics();
g2.drawImage(newImage, 0, 0, 10, 10, null);
g2.dispose();

Or you can scale the image directly by drawing it on another BufferedImage : 或者,您可以通过在另一个BufferedImage上绘制图像来直接缩放图像:

BufferedImage scaled = new BufferedImage(newWidth, newWidth, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) scaled.getGraphics();
g2.drawImage(originalImage, 0, 0, newWidth, newWidth, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
g2.dispose();

The second approach will work correctly if the two BufferedImages have the same aspect ratio. 如果两个BufferedImages具有相同的宽高比,则第二种方法将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM