Java 在不读取目标jpeg的情况下将区域(100x100px)写入大文件
在不读取整个目标图像的情况下,是否可以将区域(小100x100px)写入图像(250k x 250k px)?我的区域只有100px的正方形,我喜欢将它存储在一个巨大的Jpeg文件中的某个位置。 谢谢你的提示,Java 在不读取目标jpeg的情况下将区域(100x100px)写入大文件,java,image,image-processing,jpeg,Java,Image,Image Processing,Jpeg,在不读取整个目标图像的情况下,是否可以将区域(小100x100px)写入图像(250k x 250k px)?我的区域只有100px的正方形,我喜欢将它存储在一个巨大的Jpeg文件中的某个位置。 谢谢你的提示, Durin对于BMP这样的原始格式,您只需要知道在哪里写入即可 但JPEG是一种(有损)压缩格式。您必须使数据与压缩算法保持一致。因此,在图像中间写入内容需要算法来支持这一点。我不知道JPEG的详细信息,但我认为这不是它的一个功能。对于BMP这样的原始格式,您只需要知道在哪里写入即可 但
Durin对于BMP这样的原始格式,您只需要知道在哪里写入即可
但JPEG是一种(有损)压缩格式。您必须使数据与压缩算法保持一致。因此,在图像中间写入内容需要算法来支持这一点。我不知道JPEG的详细信息,但我认为这不是它的一个功能。对于BMP这样的原始格式,您只需要知道在哪里写入即可
但JPEG是一种(有损)压缩格式。您必须使数据与压缩算法保持一致。因此,在图像中间写入内容需要算法来支持这一点。我不知道JPEG的详细信息,但我不认为这是它的一个功能。这可能不是您想要的,但我正在添加答案,如果其他人需要解决方案。:-) ImageIO API不支持将区域写入文件。但是,这种支持是特定于格式的,正如其他答案已经指出的那样,JPEG(和大多数其他压缩格式)不是这种格式
public void replacePixelsTest(BufferedImage replacement) throws IOException {
// Should point to an existing image, in a format supported (not tested)
File target = new File("path/to/file.tif");
// Find writer, use suffix of existing file
ImageWriter writer = ImageIO.getImageWritersBySuffix(FileUtils.suffix(target)).next();
ImageWriteParam param = writer.getDefaultWriteParam();
ImageOutputStream output = ImageIO.createImageOutputStream(target);
writer.setOutput(output);
// Test if the writer supports replacing pixels
if (writer.canReplacePixels(0)) {
// Set the region we want to replace
writer.prepareReplacePixels(0, new Rectangle(0, 0, 100, 100));
// Replacement image is clipped against region prepared above
writer.replacePixels(replacement, param);
// We're done updating the image
writer.endReplacePixels();
}
else {
// If the writer don't support it, we're out of luck...
}
output.close(); // You probably want this in a finally block, but it clutters the example...
}
这可能不是您想要的,但我正在添加答案,如果其他人需要解决方案的话。:-) ImageIO API不支持将区域写入文件。但是,这种支持是特定于格式的,正如其他答案已经指出的那样,JPEG(和大多数其他压缩格式)不是这种格式
public void replacePixelsTest(BufferedImage replacement) throws IOException {
// Should point to an existing image, in a format supported (not tested)
File target = new File("path/to/file.tif");
// Find writer, use suffix of existing file
ImageWriter writer = ImageIO.getImageWritersBySuffix(FileUtils.suffix(target)).next();
ImageWriteParam param = writer.getDefaultWriteParam();
ImageOutputStream output = ImageIO.createImageOutputStream(target);
writer.setOutput(output);
// Test if the writer supports replacing pixels
if (writer.canReplacePixels(0)) {
// Set the region we want to replace
writer.prepareReplacePixels(0, new Rectangle(0, 0, 100, 100));
// Replacement image is clipped against region prepared above
writer.replacePixels(replacement, param);
// We're done updating the image
writer.endReplacePixels();
}
else {
// If the writer don't support it, we're out of luck...
}
output.close(); // You probably want this in a finally block, but it clutters the example...
}
PNG很好,只要它的格式和算法不支持此操作,任何压缩格式都可以。PNG很好,只要它的格式和算法不支持此操作,任何压缩格式都可以。如果您愿意,您必须放弃图像编解码器的有损压缩功能接下来要问的是,您是否可以简单地将原始像素存储在磁盘上?如果是,那么您可以查看hdf文件格式。它有透明的压缩机制,但不如jpeg等。我认为这是最好的,你可以get@Durin:您是否试图避免读取整个图像以节省内存、CPU(/时间)或两者兼而有之?@haraldK我试图避免读取整个目标图像。载入内存太大了。我想修改它的一部分,而不必先阅读整个图像。目前它们是以jpeg格式存储的。@Durin如果只考虑内存(Java堆),那么可以使用,用最少的堆进行替换。它的速度很慢,但可能有助于避免出现
OutOfMemoryError
的情况。如果需要,您必须放弃图像编解码器的有损压缩功能,接下来要问的是,您是否可以简单地将原始像素存储在磁盘上?如果是,那么您可以查看hdf文件格式。它有透明的压缩机制,但不如jpeg等。我认为这是最好的,你可以get@Durin:您是否试图避免读取整个图像以节省内存、CPU(/时间)或两者兼而有之?@haraldK我试图避免读取整个目标图像。载入内存太大了。我想修改它的一部分,而不必先阅读整个图像。目前它们是以jpeg格式存储的。@Durin如果只考虑内存(Java堆),那么可以使用,用最少的堆进行替换。这很慢,但可能有助于避免出现OutOfMemoryError
情况。