java HBase Playframework获取字节[]值作为图像,并在网站上显示
我正在开发Play Framework 2.2.6、HBase 0.94.27和Solr 5.5.0(我的问题中没有显示任何Solr代码)。我试图将pdf的第一页呈现为图像,并像amazon或ebay那样在搜索列表中显示该图像。我首先解析图像并将其转换为字节[],以便将其存储在HBase中
我可以在HBase中成功地将渲染图像索引为字节,但无法在html端显示它。我收到错误:Error found: javax.imageio.IIOException: Can't read input file!
该程序将第一页呈现为图像,并将is转换为byte[] byteImage
,并在HBase中索引该字节变量
搜索部分:
final static List<Searching> searchList = new ArrayList<Searching>();
//Searching class has string variables for title, content, number of pages and picture.
searchList.clear();
Form<Searching> filledForm = searchForm.bindFromRequest();
Searching searched = filledForm.get();
try {
HTable hTable = new HTable(hConn.config, "books");
Get g = new Get(Bytes.toBytes(searched.outputId));
g.addColumn(Bytes.toBytes("picture"), Bytes.toBytes("raw"));
org.apache.hadoop.hbase.client.Result result = hTable.get(g);
if (result.containsColumn(Bytes.toBytes("picture"), Bytes.toBytes("raw"))) {
byte[] byteOutputPicture = result.getNoVersionMap().get(Bytes.toBytes("picture")).get(Bytes.toBytes("raw"));
BufferedImage image = ImageIO.read(new File(byteOutputPicture+".png"));
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] res=baos.toByteArray();
searched.outputPicture = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(baos.toByteArray());
//outputPicture is a String variable in Searching class.
}
searchList.add(searched);
hTable.close();
} catch (Exception e) {
System.out.println("Error found: " + e);
}
搜索。斯卡拉。html:
@(searchList: List[Searching])
...
<body>
@for(search <- searchList) {
<img src="data:image/png;base64,@search.outputPicture" alt="IMG">
}
</body>
更新1:我更改了html端,并根据this website添加了一些代码,但我仍然得到Error found: java.lang.NullPointerException
。我无法确定错误来自何处,因为程序不知何故停止了
更新2:更改一些代码后,出现了新的错误:Error found: javax.imageio.IIOException: Can't read input file!
# 1 楼答案
我发现我需要在
read()
函数中使用ByteArrayInputStream()
而不是File()
Result on my web page