如何将hadoop中的sequencefile转换成图像文件?以下代码返回错误(bimagefromconvert为空)

iezvtpos  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(270)
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);

//序列文件的键是图像名称,值是图像内容。

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

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

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

//由于bimagefromconvert为空,下面一行返回错误

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

de90aj5v1#

你的价值观(形象)应该是可写的。你应该像:

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"));
    }
}

那就行了

相关问题