我正在尝试在中使用QuantizFilter

http://www.jhlabs.com/ip/filters/index.html

减小屏幕截图的颜色深度。

这是我非常简单的代码:

    Robot robo = new Robot();
    BufferedImage notQuantized = robo.createScreenCapture( new Rectangle ( 0, 0, 300, 300 ) );
    BufferedImage Quantized = new BufferedImage( 300, 300, BufferedImage.TYPE_INT_BGR);
    File nonquantized = new File ("C:\\nonquantized.png");
    File quantized = new File("C:\\quantized.png");
    nonquantized.createNewFile();
    quantized.createNewFile();
    QuantizeFilter bla = new QuantizeFilter();

    int [] outPixels = new int[300*300*3];
    int [] inPixels = new int[300*300*3];

    notQuantized.getRaster().getPixels( 0, 0, 300, 300, inPixels );
    bla.quantize( inPixels, outPixels, 300, 300,2, true, true );

    Quantized.getRaster().setPixels( 0, 0, 300, 300, outPixels );
    ImageIO.write( Quantized, "png", quantized );
    ImageIO.write( notQuantized, "png", nonquantized );
. 但是,我剩下的是:

原始IMG:

量化IMG:

对问题的进一步分析表明,inpixels阵列被错误地填写;它充满了原始图像的上三分之一的三次。

任何指针如何修复它?

另外,Java中的任何链接良好+快速量化算法?我搜索的是一种算法,它将采用type_int_bgr映像,并生成新的type_int_bgr图像,但具有较少的实际差异,因此可以很容易地放气。 例如,如果我们在原始图像中有两个像素,则值为255,255,234,另一个具有值为255,255,236,它们都应该转换为255,255,240。干杯

有帮助吗?

解决方案

The following example will convert your image correctly:

QuantizeFilter q = new QuantizeFilter();
int [] inPixels = new int[image.getWidth()*image.getHeight()*3];
int [] outPixels = new int[image.getWidth()*image.getHeight()*3];
image.getRaster().getPixels( 0, 0, image.getWidth(), image.getHeight(), inPixels );
q.quantize(inPixels, outPixels, image.getWidth(), image.getHeight(), 64, false, false);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0,0,width,height,outPixels);

Also there is no reason to create the files implicitly, as ImageIO.write creates them automatically.

其他提示

I have had the same problem and its not the code you posted that's at fault it's the QuantizeFilter class which doesn't go through all the pixels. You need to find this code part

 if (!dither) {
        for (int i = 0; i < count; i++)
            outPixels[i] = table[quantizer.getIndexForColor(inPixels[i])];

and multiply count by 3.

Please note that this is only a a fix if the last 2 parameters are set to false.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top