HBase - Playframework 获取 byte[] 值作为图像并在网站上显示

HBase - Playframework get byte[] value as image and show it on website

我正在研究 Play Framework 2.2.6、HBase 0.94.27 和 Solr 5.5.0(我没有在我的问题中显示任何 solr 代码。)。我正在尝试将 pdf 的第一页呈现为图像,并像 amazon or ebay 那样在搜索列表中显示该图像。我先解析图片,转成byte[],存入HBase。

我可以在 HBase 中成功地将渲染图像索引为字节,但我无法在 html 端显示它。我收到错误消息:Error found: javax.imageio.IIOException: Can't read input file! .

程序将第一页呈现为图像并将其转换为 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);
 }

search.scala.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。我不能确定错误来自哪里,因为程序不知何故停止了。

更新二:修改部分代码后出现新错误:Error found: javax.imageio.IIOException: Can't read input file!

我发现我需要在 read() 函数中使用 ByteArrayInputStream() 而不是 File()

Result on my web page