java - Converting an image to byte[] by using PDFBox -


i using pdfbox 2.0. while parsing pdf document, want first page image , store hbase using in search results(i going create search list page search page of amazon.com).

hbase accepts byte[] variable store(index) value. need convert image byte[], store hbase. have implemented image render, how can convert byte[]?

        pddocument document = pddocument.load(file, "");         bufferedimage image = null;         try {             pdfrenderer pdfrenderer = new pdfrenderer(document);             if (document.isencrypted()) {                 try {                     system.out.println("trying decrypt...);                     document.setallsecuritytoberemoved(true);                     system.out.println("the file has been decrypted in .");                 }                 catch (exception e) {                     throw new exception("cannot decrypted. ", e);                 }             }             pdpage firstpage = (pdpage) document.getdocumentcatalog().getpages().get(0);             pdfrenderer.renderimagewithdpi(0, 300, imagetype.rgb);                // 0 means first page.              image = pdfrenderer.renderimagewithdpi(0, 300, imagetype.rgb);                               document.close();      } catch (exception e) {             e.printstacktrace();     }  

if write imageioutil.writeimage(image , filename+".jpg" ,300); above right above document.close();, program creates jpg file in project path. need put in byte[] array instead of creating file. possible?

this can done imageio.write(image, string, outputstream) can write arbitrary outputstream rather disk. bytearrayoutputstream can store output bytes array in memory.

import java.io.bytearrayoutputstream; ... // example image bufferedimage image = new bufferedimage(4, 3, bufferedimage.type_int_argb);  // array bytearrayoutputstream bos = new bytearrayoutputstream(); imageio.write(image, "jpg", bos); byte [] output = bos.tobytearray(); system.out.println(arrays.tostring(output)); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -