JAVA编写灰度JPEG图像(依赖于JDK版本?)
我是JAVA语言的初学者,需要使用代码(编写灰度JPEG图像)。 我找了很长时间,但我现在不知道是什么问题JAVA编写灰度JPEG图像(依赖于JDK版本?),java,image-processing,jpeg,raster,Java,Image Processing,Jpeg,Raster,我是JAVA语言的初学者,需要使用代码(编写灰度JPEG图像)。 我找了很长时间,但我现在不知道是什么问题 publicstaticbooleanwriteImage(stringinputfilename,stringoutputfilename,int[]imageData) { BuffereImage inputImage=MyImageReader.ReadImageInToBuffereImage(inputFileName); if(inputImage==null) { Syst
publicstaticbooleanwriteImage(stringinputfilename,stringoutputfilename,int[]imageData)
{
BuffereImage inputImage=MyImageReader.ReadImageInToBuffereImage(inputFileName);
if(inputImage==null)
{
System.out.println(“无法打开输入图像”);
返回false;
}
BuffereImage outputImage=新的BuffereImage(inputImage.getWidth(),inputImage.getHeight(),
inputImage.getType());
可写光栅输出器,输入器;
InputMaster=inputImage.getRaster();
OutputMaster=inputMaster.createCompatibleWritableRaster();
int波段=0;
int numbands=outputRaster.getNumBands();
int高度、宽度;
高度=OutputMaster.getHeight();
宽度=OutputMaster.getWidth();
int[]pixelData=新int[1];
对于(int y=0;y
我正在使用上面的代码。
代码似乎通过从输入jpeg文件复制属性来创建一个可写文件,
因此,输出的jpeg文件将具有相同的大小
我正在做的是对灰度光栅值进行一些图像处理
并将其另存为jpeg文件
当我用简单的4x4箱测试它时,结果如下
要保存在(高度、宽度)坐标中的输入数组
1234
56778
15 16 17 18
25 26 27 28
当我加载保存的文件时,它以(高度、宽度)坐标显示
2 3 4 5
3 4 5 6
15 16 17
25 26 27 28
这怎么可能?我想不出是怎么回事。
图像阅读器被证明是安全的。
我在Oracle的JDK1.7上运行,但原始代码可能是在过去编写的。
但我希望这不是问题所在
谢谢大家!如果你能够使用第三方库并决定使用(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;
}
关键是方法调用(将预定义的op应用于图像)。如果没有一些示例数据,我真的不能这么说。您调试并查看了吗?还有,为什么只有一次迭代有一个for循环
for(band=0;band<1;band++){pixelData[0]=imageData[0][y][x];}
。所以我将其抑制为0。我在MATLAB中使用imwrite(矩阵,'filename'jpg')做了同样的事情,它显示了类似的结果。我做错什么了吗?我假设数组[height][width]可以保存为jpg文件(通过一些编码),问题一定是在循环中,或者是传递给函数的数据。就我所知,在循环中没有什么是错误的。但是如果没有调试,我很难判断。你是否尝试过用调试器检查代码??我做的一件事是检查要传递的值,设置像素数据。我无法检查imageBuffer(我必须学习如何检查)。但我在MATLAB中也经历了同样的事情。我真的可以通过发送代表索引值的二维数组来编写灰度jpeg图像吗?谢谢你的回答!但我发现即使我使用Scalr库,我也看到了同样的结果。我想做的只是,将2D数组另存为jpeg文件。我假设jpeg文件将uint值写入2D坐标给定的每个单元格。因此,我希望如果我为坐标(0,0)指定了一个值255并保存它,那么下次加载它时,应该在(0,0)处检索相同的值,但它不是。