2013-03-05 74 views
0

我想構建一個mapreduce代碼來讀取和寫入圖像,但我使用ImageIO.write保存圖像時得到ArrayIndexOutOfBoundsException,但我沒有做任何更改圖像。這裏是代碼: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) 
+1

也請給我們堆棧跟蹤。 – Amar 2013-03-05 19:18:30

+0

Amar錯誤信息對您有幫助嗎? – user1602717 2013-03-06 16:05:54

+0

所以從查看堆棧跟蹤看來,在閱讀時引發異常,而不是像您在問題中提到的那樣寫入異常!你應該看的地方是'at imagetojpeg.KUImage.readFields(KUImage.java:33)' – Amar 2013-03-06 18:25:24

回答

-1

我就遇到了這個問題,它原來是間我會怎樣聲明輸出文件和輸入文件不匹配。在我之前的步驟中,我將輸出文件定義爲SequenceFileAsBinaryOutputFormat,但在下一步中,我聲明輸入爲SequenceFileInputFormat。一旦我將輸入格式更改爲SequenceFileAsBinaryInputFormat,問題就消失了。