Java SWT:从Image获取像素子集以创建新图像

时间:2016-06-07 22:40:21

标签: java image image-processing swt pixel

我正在显示像素列表中的图像,如下所示(这有效):

private Image GetImage()
{
    PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
    ImageData imageData = new ImageData(bmpHW, bmpHW,32,palette);

    currentImagePixelVec = getPixelsFromBMP(0, 0, graphicsMemory);
    int pixelVecLoc=0;
    for (int h = 0; h<bmpHW; h++)
    {
        for (int w = 0; w<bmpHW; w++)
        {
            int p = 0;
            Pixel pixel = currentImagePixelVec.get(pixelVecLoc);
            p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;                
            imageData.setPixel(w, h, p);

            pixelVecLoc++;
        }
    }


    imageData = imageData.scaledTo(700, 700);
    Image image = ImageDescriptor.createFromImageData(imageData).createImage();
    return image;

}

我让用户选择Image的矩形如下(这可行):

                if(drag)
                {
                    GC gc1 = e.gc;

                    //gc.setBackground(top.getDefault().getSystemColor(SWT.COLOR_BLACK));
                    gc1.setAlpha(128);

                    int minX = Math.min(startX, endX);
                    int minY = Math.min(startY, endY);

                    int maxX = Math.max(startX, endX);
                    int maxY = Math.max(startY, endY);

                    int width = maxX - minX;
                    int height = maxY - minY;

                    gc1.fillRectangle(minX, minY, width, height);
                }

我想从矩形选择中创建一个新图像:

private Image GetZoomedImage()
{                       

int minX = Math.min(startX, endX);
int minY = Math.min(startY, endY);

int maxX = Math.max(startX, endX);
int maxY = Math.max(startY, endY);

int width = maxX - minX;
int height = maxY - minY;

    PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
    ImageData imageData = new ImageData(1300, 1300,32,palette);

    int newHeight = 0;
    int newWidth = 0;
    for (int h = minX; h<maxX; h++)
    {
        for (int w = minY; w<maxY; w++)
        {           
            int p = 0;
            //Pixel pixel = currentImagePixelVec.get((h * w)-1);
            int actualPix = imageDisplayed.getImageData().getPixel(h, w);
            //p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;      
            System.out.println("Pixel: " + Integer.toString(actualPix));
            //imageData.setPixel(newWidth,newHeight, p);
            imageData.setPixel(newWidth,newHeight, actualPix);

            newWidth++;
        }
        newHeight++;
    }


    imageData = imageData.scaledTo(700, 700);
    Image image = ImageDescriptor.createFromImageData(imageData).createImage();
    return image;

}

我是否正确地选择了这个?

1 个答案:

答案 0 :(得分:1)

您应该只获取现有图像的图像数据,因为这可能是一项昂贵的操作:

ImageData imageDisplayedData = imageDisplayed.getImageData();

您的新图像数据应该只是您想要的宽度和高度:

ImageData imageData = new ImageData(width, height, 32, palette);

您可以使用

public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex)

public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex)

方法ImageData一次读取和写入整行的数据:

int [] pixels = new int [width];

int newRow = 0;
for (int row = minY; row < maxY; row++)
 {           
   imageDisplayedData.getPixels(minX, row, width, pixels, 0);

   imageData.setPixels(0, newRow, width, pixels, 0);

   newRow++;
 }