我正在尝试构建mapreduce代码来读取和写入图像,但是在使用ImageIO.write保存图像时我得到ArrayIndexOutOfBoundsException
,但是,我没有对图像进行任何更改。这是代码:
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)
答案 0 :(得分:-1)
我遇到了这个问题,结果证明我是如何声明输出文件和输入文件之间的不匹配。在我之前的步骤中,我将输出文件定义为SequenceFileAsBinaryOutputFormat
类型,但在下一步中,我将输入声明为SequenceFileInputFormat
类型。一旦我将输入格式更改为SequenceFileAsBinaryInputFormat
,问题就消失了。