使用imageio.write保存图像时发生arrayindexoutofboundsexception

tyky79it  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(544)

我试图构建一个mapreduce代码来读写一个图像,但是我得到了 ArrayIndexOutOfBoundsException 使用imageio.write保存图像时,我不会对图像进行任何更改。代码如下:

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import java.io.BufferedInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.Writable;

public class mrimage implements Writable {
private static final Log log = LogFactory.getLog(mrimage.class);

BufferedImage reader = null;
String FileName;

public mrimage(){

}

public mrimage(BufferedImage buff,String fileName){
    this.reader = buff;
    this.FileName = fileName;
}

public BufferedImage getImage(int i){
return this.reader;
}

//reading generated image files
@Override
public void readFields(DataInput in) throws IOException { 
    try{
        reader = ImageIO.read(new BufferedInputStream((InputStream)in));

    }
    catch(Exception e){
        log.info(e);
    }
}

//writing the image files 
@Override
public void write(DataOutput out) throws IOException {

    try{
          ImageIO.write(reader, "jpg", (OutputStream)out);

    }
    catch(Exception e){
    }
}
}

有人能帮忙吗?
这是我收到的完整错误消息:

java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.ByteArrayInputStream.read(ByteArrayInputStream.java:174)
at java.io.DataInputStream.read(DataInputStream.java:132)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at javax.imageio.stream.FileCacheImageInputStream.readUntil(FileCacheImageInputStream.java:121)
at javax.imageio.stream.FileCacheImageInputStream.read(FileCacheImageInputStream.java:167)
at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:337)
at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:347)
at com.sun.imageio.plugins.bmp.BMPImageReaderSpi.canDecodeInput(BMPImageReaderSpi.java:66)
at javax.imageio.ImageIO$CanDecodeInputFilter.filter(ImageIO.java:541)
at javax.imageio.spi.FilterIterator.advance(ServiceRegistry.java:793)
at javax.imageio.spi.FilterIterator.<init>(ServiceRegistry.java:787)
at javax.imageio.spi.ServiceRegistry.getServiceProviders(ServiceRegistry.java:491)
at javax.imageio.ImageIO.getImageReaders(ImageIO.java:620)
at javax.imageio.ImageIO.read(ImageIO.java:1412)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at imagetojpeg.KUImage.readFields(KUImage.java:33)
at org.apache.hadoop.io.serializer.WritableSerialization$WritableDeserializer.deserialize(WritableSerialization.java:67)
at org.apache.hadoop.io.serializer.WritableSerialization$WritableDeserializer.deserialize(WritableSerialization.java:40)
at org.apache.hadoop.mapreduce.ReduceContext.nextKeyValue(ReduceContext.java:116)
at org.apache.hadoop.mapreduce.ReduceContext.nextKey(ReduceContext.java:92)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:175)
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:649)
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:417)
at org.apache.hadoop.mapred.Child$4.run(Child.java:261)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1059)
at org.apache.hadoop.mapred.Child.main(Child.java:255)
dffbzjpn

dffbzjpn1#

我遇到了这个问题,结果是我声明输出文件和输入文件的方式不匹配。在上一步中,我将输出文件定义为 SequenceFileAsBinaryOutputFormat ,但在下一步中,我将输入声明为 SequenceFileInputFormat . 一旦我把输入格式改成 SequenceFileAsBinaryInputFormat 问题消失了。

相关问题