繁体   English   中英

JAVA编写灰度JPEG图像(取决于JDK版本吗?)

[英]JAVA Writing Gray Scale JPEG Image (dependency on JDK version?)

我是JAVA语言的初学者,我需要使用代码(编写灰度JPEG图像)。 我搜索了很长时间,但现在不知道出了什么问题。

public static boolean writeImage(String inputFileName, String outputFileName, int[][][] imageData)
{
    BufferedImage inputImage = MyImageReader.readImageIntoBufferedImage( inputFileName );
    if ( inputImage == null )
    {
        System.out.println(" Could not open input image.");
        return false;
    }

    BufferedImage outputImage = new BufferedImage( inputImage.getWidth(), inputImage.getHeight(),
                                                   inputImage.getType() );
    WritableRaster outputRaster, inputRaster;
    inputRaster = inputImage.getRaster();
    outputRaster = inputRaster.createCompatibleWritableRaster();

     int band = 0;
    int numbands = outputRaster.getNumBands();

    int height, width;
    height = outputRaster.getHeight();
    width = outputRaster.getWidth();
    int[] pixelData = new int[ 1 ];

    for ( int y = 0; y < height; y++ )
        for ( int x = 0; x < width; x++ )
        {
            for ( band = 0; band < 1; band++ )
            {
                pixelData[ 0 ] = imageData[0][y][x];
            }
            outputRaster.setPixel(x, y, pixelData );
        }

    outputImage.setData( outputRaster );

    File outputFile = new File( outputFileName );
    try
    {
        if ( !ImageIO.write( outputImage, "jpg", outputFile ))
        {
            System.out.println("Could not find image format for output image.");
            return false;
        }
    }
    catch ( Exception e )
    {
        System.out.println("Could not write output file.");
        return false;
    }

    return true;
}

我正在使用上面的代码。 该代码似乎是通过复制输入jpeg文件中的属性来创建WritableRaste的,因此输出jpeg文件的大小将相同。

我正在做的是对灰度光栅值进行一些图像处理,并将其另存为jpeg文件。

当我在简单的4x4情况下对其进行测试时,结果如下。

输入数组要保存在(高度,宽度)坐标中

1 2 3 4

5 6 7 8

15 16 17 18

25 26 27 28

加载保存的文件时,它显示在(高度,宽度)坐标中

2 3 4 5

3 4 5 6

15 15 16 17

25 26 27 28

这怎么可能? 我不知道怎么了。 图像阅读器被证明是安全的。 我正在Oracle的JDK 1.7上运行,但是原始代码可能是过去编写的。 但我希望这不会成为问题。

谢谢!

如果您能够使用第三方库并决定利用imgscalr (Apache 2,开放源代码和tiny-一个类),则可以在实现过程中将所有复杂的代码切掉 ,如下所示:

import org.imgscalr.Scalr.*;

public static boolean writeImage(String inputFileName, String outputFileName)
{
    BufferedImage inputImage = MyImageReader.readImageIntoBufferedImage( inputFileName );
    if ( inputImage == null )
    {
        System.out.println(" Could not open input image.");
        return false;
    }

    // This replaces everything in the middle of your original impl.
    BufferedImage outputImage = apply(inputImage, Scalr.OP_GRAYSCALE);

    File outputFile = new File( outputFileName );
    try
    {
        if ( !ImageIO.write( outputImage, "jpg", outputFile ))
        {
            System.out.println("Could not find image format for output image.");
            return false;
        }
    }
    catch ( Exception e )
    {
        System.out.println("Could not write output file.");
        return false;
    }

    return true;
}

关键是apply方法调用(将预定义的GREYSCALE op应用于图像)。

暂无
暂无

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

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