1
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.imageio.ImageIO;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.util.ReflectionUtils;



public class SeqToImage {

/* 将序列文件转换为图像 正在使用的序列文件是从图像生成的;.png 格式 */

public static void main(String args[]) throws Exception {

        Configuration confHadoop = new Configuration();       
        FileSystem fs = FileSystem.get(confHadoop);
        Path inPath = new Path("/home/Desktop/1.seq");
        SequenceFile.Reader reader = new SequenceFile.Reader(fs, inPath, confHadoop);
        Writable key = (Writable)
        ReflectionUtils.newInstance(reader.getKeyClass(), confHadoop);
        Writable value = (Writable)
        ReflectionUtils.newInstance(reader.getValueClass(), confHadoop);
        reader.next(key,value);

// 序列文件的key为图片名称,value为图片内容。

  System.out.println("KEY "+key.toString());
            byte[] b = Bytes.toBytes(value.toString());
            System.out.println(b.length);

// 输出是一些字节数,这意味着 b 不是 NULL

BufferedImage bImageFromConvert = ImageIO.read(new ByteArrayInputStream(b));

// 以下行返回错误,因为 bImageFromConvert 为 NULL

System.out.println((bImageFromConvert.toString()).length());
        ImageIO.write(bImageFromConvert, "png", new File(
                "/home/Desktop/imageAgain.png"));
        }
    }
4

1 回答 1

1

您的值(图像)应该由 BytesWritable 提供。你应该像这样投:

     BytesWritable value = (BytesWritable)
    ReflectionUtils.newInstance(reader.getValueClass(), confHadoop);

其次,您应该使用 copyBytes() 而不是 Bytes.toBytes

它给出的字节副本恰好是数据的长度。所以这应该有效

    Configuration confHadoop = new Configuration();       
    FileSystem fs = FileSystem.get(confHadoop);
    Path inPath = new Path("/home/Desktop/1.seq");
    SequenceFile.Reader reader = new SequenceFile.Reader(fs, inPath, confHadoop);
    //Name of your Image
    Text key = (Text)
    ReflectionUtils.newInstance(reader.getKeyClass(), confHadoop);
    // Value -- mage
    BytesWritable value = (BytesWritable)
    ReflectionUtils.newInstance(reader.getValueClass(), confHadoop);
    reader.next(key,value);
    .......
                byte[] b = copyBytes(value) ;

    System.out.println((bImageFromConvert.toString()).length());
    ImageIO.write(bImageFromConvert, "png", new File(
            "/home/Desktop/imageAgain.png"));
    }
}

那会做

于 2014-01-03T11:51:21.253 回答