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"));
}
}
1条答案
按热度按时间de90aj5v1#
你的价值观(形象)应该是可写的。你应该像:
应该使用copybytes()而不是bytes.tobytes
它给出了一个字节的拷贝,这个字节正好是数据的长度。所以这应该管用
那就行了