更宽/更高的图像变形

时间:2012-10-07 22:00:27

标签: java

我正在使用C ++加载图像并通过ByteBuffer将像素提供给JNI。我知道像素正好被喂食,因为如果图像是方形的,它们会呈现完美的效果。如果它们是矩形的,它们会变形。我还成功地将图像保存在DLL中并且它可以工作。不幸的是,Java放弃了我(除非它像方块一样)。我无法弄清楚为什么!我究竟做错了什么?

package library;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Frame extends JFrame {

    public Frame(int Width, int Height, String FrameName, BufferedImage Buffer) {
        setName(FrameName);
        setSize(Width, Height);
        getContentPane().add(new JLabel(new ImageIcon(Buffer)));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
}

所有装载:

package library;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.nio.ByteBuffer;

class SharedLibrary {

    static{System.loadLibrary("TestDLL");} 
    private static native void GetGLBuffer(ByteBuffer Buffer);

    private ByteBuffer Buffer = null;    
    private int ByteSize = 0, Width = 0, Height = 0, BitsPerPixel = 32;

    public SharedLibrary(int ImageWidth, int ImageHeight) throws IOException {

        Width = ImageWidth;
        Height = ImageHeight;

        ByteSize = ((Width * BitsPerPixel + 31) / 32) * 4 * Height;     //Compute Image Size in Bytes.
    Buffer = ByteBuffer.allocateDirect(ByteSize);                   //Allocate Space for the image data.

        GetGLBuffer(Buffer);                                            //Fill the buffer with Image data from the DLL.

        byte[] Bytes = new byte[ByteSize];
        Buffer.get(Bytes);

        BufferedImage Image = new BufferedImage(Width, Height, BufferedImage.TYPE_3BYTE_BGR);
        WritableRaster raster = (WritableRaster) Image.getData();
        raster.setPixels(0, 0, Width, Height, ByteBufferToIntBuffer(Bytes));
        Image.setData(raster);

        Frame F = new Frame(Width, Height, "", Image);
    }

    private int[] ByteBufferToIntBuffer(byte[] Data) {
        int IntBuffer[] = new int[Data.length];
        for (int I = 0; I < Data.length; I++) {
            IntBuffer[I] = (int)Data[I] & 0xFF;
        }
        return IntBuffer;
    }
}

enter image description here 上面的Image完美绘制,因为它几乎是正方形。如果我将其调整为矩形,则会变形。例如:

enter image description here

变形并看起来像: enter image description here

0 个答案:

没有答案