JAVA写入灰度JPEG图像(依赖JDK版本?)

标签 java image-processing jpeg raster

我是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 上运行,但原始代码可能是过去编写的。 但我希望这不会成为问题。

谢谢!

最佳答案

如果您能够使用第 3 方库并决定利用 imgscalr (Apache 2,开源且微型 - 一个类)您可以在实现过程中删除所有复杂的代码,它看起来像这样:

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方法调用(将预定义的 GRAYSCALE op 应用于图像)。

关于JAVA写入灰度JPEG图像(依赖JDK版本?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13004388/

相关文章:

java - 为什么Java ImageIcon不进入catch子句?

java - 在Java中获取**之间的文本

java - 将变量分配给 null 的最佳替代方法是什么?

python - 如何在 Python 中保存 HSV 转换后的图像?

video - 从 JPG 创建一个视频,没有以 ffmpeg 开头的所有图片

java - 如何从 ArrayList 中删除特定数组

C++ OpenCV : Iterate through pixels in a Mat which is ROI of another Mat

python - 在python中创建圆圈来掩盖图像并计算每个圆圈内的像素

python - 使用 PIL 裁剪 jpeg 图像是无损的吗?

android - 以编程方式检查 Android 上的运动 jpeg 图像