I am trying to create a document with htmlWriter in com.lowagie.text in Java.
What I do, is that I create an image (from qr-code) and try to add it to the document.
The document is connected to an ByteArrayOutputStream, and then I write it out to a ServletOutputStream.
When I create an image from bitmatrix, nothing happens.
I wonder if this is because the html need an image-URL. So If I get the image from url, it shows. But when I just create an image in java, it will not display this in the html?!?
Can anyone help me?
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// setting some response headers
response.setHeader("Expires", EXPIRES);
// setting the content type
response.setContentType(CONTENT_TYPE);
ServletOutputStream out = null;
ByteArrayOutputStream baos = null;
try {
baos = getHtmlTicket();
// write ByteArrayOutputStream to the ServletOutputStream
out = response.getOutputStream();
baos.writeTo(out);
}
catch (Exception e) {
log.error(e.getMessage(), e);
response.setContentType("text/html");
// response.setHeader("Content-Disposition", "filename=\"" + filename + "\"");
response.getWriter().write("<p>Det har oppst�tt en feil!</p>");
response.getWriter().write("<p>" + new Date().toString() + "</p>");
response.getWriter().write("<!-- " + e.getMessage() + " -->");
response.flushBuffer();
}
public ByteArrayOutputStream getHtmlTicket() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
String myCodeText = "YO YOU";
int size = 128;
try {
HtmlWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
int pictureWidth = byteMatrix.getWidth();
BufferedImage bimage = new BufferedImage(pictureWidth, pictureWidth,
BufferedImage.TYPE_INT_RGB);
bimage.createGraphics();
Graphics2D graphics = (Graphics2D) bimage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, pictureWidth, pictureWidth);
graphics.setColor(Color.BLACK);
for (int i = 0; i < pictureWidth; i++) {
for (int j = 0; j < pictureWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(bimage , null);
document.add(image);
}
catch (DocumentException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
return baos;
}
HtmlWriter was created to test the Itext library during development. That is why the image only display as a square without content. That is also why the creators of Itext have removed htmlWriter it in later versions.
If you want the response to display the image (must be bufferedImage) in HTML, you can do so by converting the image to Base64 like this:
private String addImageToHTML(BufferedImage bf) {
String base64String = "";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bf, "png", baos);
base64String = DatatypeConverter.printBase64Binary(baos.toByteArray());
}
catch (IOException e) {
e.printStackTrace();
}
return "<img style='max-width:100%' src='data:image/png;base64,"+ base64String + "' alt='IMG DESC'/>";
}
Related
I'm creating a simple stream to send images taken from client's screen from client to server. For now I can receive the first image but then the app crashed unexpectedly. The idea is send the size and the image in byte array, the server receive that byte array and convert to image.
FromClient:
public void run() {
image = new BufferedImage(NORM_PRIORITY, MIN_PRIORITY, MAX_PRIORITY);
while(continueLoop) {
//send captured screen
image = robot.createScreenCapture(rectangle);
try {
//Initiate the stream
OutputStream out = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
// store the size of each image
byte[] size = ByteBuffer.allocate(4).putInt(baos.size()).array();
dos.write(size);
dos.write(baos.toByteArray(), 0, baos.toByteArray().length);
dos.flush();
} catch(IOException e) {
e.printStackTrace();
e.printStackTrace();
continueLoop = false;
}
try {
Thread.sleep(30);
} catch(InterruptedException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
ToServer:
public void run() {
boolean continueLoop = true;
try {
drawGUI();
// Initiate the stream
InputStream is = clientSocket.getInputStream();
DataInputStream configGraphicStream = new DataInputStream(is);
BufferedImage image = null;
while(continueLoop) {
//receive the size of image and convert to type int
byte[] sizeInByte = new byte[64];
configGraphicStream.read(sizeInByte);
int length = ByteBuffer.wrap(sizeInByte).asIntBuffer().get();
try {
// Get images
byte[] img = new byte[length];
configGraphicStream.readFully(img, 0, img.length);
image = ImageIO.read(new ByteArrayInputStream(img));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
//draw images
if( image != null)
{
Graphics graphics = clientPanel.getGraphics();
graphics.drawImage(image, 0, 0, clientPanel.getWidth(), clientPanel.getHeight(), clientPanel);
}
System.out.println("Receiving image");
Thread.sleep(30);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Please help me solve this problem.
I got the problem that my program is stuck in the for loop because the dataInputSteam doesn't receive all data before the DataOutputSteam is finished.
In my program I want to send a secreenshot with the server and the client should receive it:
Server:
public sendScreen(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
Robot robot;
try {
robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screen = new Rectangle( screenSize );
BufferedImage image = robot.createScreenCapture( screen );
BufferedImage scaledImage = Scalr.resize(image, 300);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] screenBytes = baos.toByteArray();
daos = new DataOutputStream(socket.getOutputStream());
daos.writeInt(screenBytes.length);
daos.write(screenBytes);
System.out.println("Screen sent");
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And the client:
public static class GetScreenshot implements Runnable{
Socket socket;
private static DataInputStream din;
private static BufferedImage screenshot;
public GetScreenshot(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
System.out.println("start method getScreenshot");
int length;
byte[] buffer;
PrintWriter out;
try {
//sending command to send screenshot
out = new PrintWriter(socket.getOutputStream(), true);
out.println("GETSCREENSHOT");
din = new DataInputStream(socket.getInputStream());
System.out.println("DIS created");
length = din.readInt();
System.out.println("Got data from DIS");
buffer = new byte[length];
System.out.println("Filled buffer");
for(int i = 0; i < length; i++){
buffer[i] = (byte) din.read();
System.out.println("read" + i+ "while length is " + length + " read data " + buffer);
}
System.out.println("got buffer");
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
screenshot = ImageIO.read(bais);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//writing screenshot to local disk
File outputfile = new File("C:\\users\\XXXX\\documents\\image2.png");
try {
ImageIO.write(screenshot, "png", outputfile);
System.out.println("image written to local disk");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//set screenshot in the tool
Main.labelScreenshot.setIcon(new ImageIcon(screenshot));
}
}
Does anybody know how i can transfer all of the data of the screenshot?
Greetings
Max
I think your problem is because your view (canvas/label/etc) width & height is smaller than your image. Try to resize the screenshoot so the width & height is same as your view (canvas/label/etc).
buffer = new byte[length];
System.out.println("Filled buffer");
At this point this message is simply untrue. You have created the buffer, but you certainly haven't filled it.
for(int i = 0; i < length; i++){
buffer[i] = (byte) din.read();
System.out.println("read" + i+ "while length is " + length + " read data " + buffer);
}
All this is equivalent to:
din.readFully(buffer);
And then:
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
screenshot = ImageIO.read(bais);
//writing screenshot to local disk
File outputfile = new File("C:\\users\\XXXX\\documents\\image2.png");
try {
ImageIO.write(screenshot, "png", outputfile);
All this is entirely equivalent to:
try (new FileOutputStream out = new FileOutputStream("C:\\users\\XXXX\\documents\\image2.png"))
{
out.write(buffer);
}
There is no need to decode and re-encode the image.
You add this to the sender part:
socket.flush();
if not worked, then:
socket.shutdownOutput();
I am currently storing the tiff image in BufferredImage during runtime and displaying the same in image src tag of hmtl.
My tiff image size is about 60kb and currently it takes approx 1 sec time to load in web browser.
Is there any way to compress the tiff image or BufferedImage so that the time to load the image in browser can be faster.
Below is my code for saving tiff image in BufferredImage.
public BufferedImage savetiff(File srcFilePath) throws IOException {
FileSeekableStream stream = new FileSeekableStream(srcFilePath);
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
ParameterBlock params = new ParameterBlock();
params.add(stream);
RenderedOp image1 = JAI.create("tiff", params);
BufferedImage img = image1.getAsBufferedImage();
return img;
}
Code for converting tiff to image but in this code I have to save the jpeg image in disk and then I have to read it again. If there is any way to convert tiff to JPEG and save in BufferedImage.
public boolean tiff2JPG(File srcFilePath, File destFilePath) {
boolean status = false;
SeekableStream s = null;
RenderedImage op = null;
ImageDecoder dec = null;
TIFFDecodeParam param = null;
FileOutputStream fos = null;
JPEGEncodeParam jpgparam = null;
ImageEncoder en = null;
try {
s = new FileSeekableStream(srcFilePath);
dec = ImageCodec.createImageDecoder("tiff", s, param);
op = dec.decodeAsRenderedImage(0);
fos = new FileOutputStream(destFilePath);
jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
status = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
} catch (IOException io) {
// TODO Auto-generated catch block
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return status;
Please advice.
Regards,
Dinesh Pise
I want to create image from first page of an PDF . I am using iText in java . Can you suggest me what to do to extract first page of an pdf as an image ?
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(
document, new FileOutputStream(RESULT));
document.open();
File extStore = Environment.getExternalStorageDirectory();
String path=extStore.getPath()+"/FirstPdf.pdf";
PdfReader reader = new PdfReader(path);
int n = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
page = writer.getImportedPage(reader, i);
// Image.getInstance(page) ;
}
document.close();
I have written the above code . What to do to extract first page of a pdf as an image and save it in SDCARD ?
iText doesn't work for that purpose.
http://www.java2s.com/Open-Source/Android_Free_Code/Pdf/Download_Free_code_Android_Pdf_Viewer_Library.htm
The jar file is in the zip.
Download that library PdfViewer.jar and try this code:
byte[] bytes;
try {
File file = new File("/storage/extSdCard/Test.pdf");
FileInputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
ByteBuffer buffer = ByteBuffer.NEW(bytes);
String data = Base64.encodeToString(bytes, Base64.DEFAULT);
PDFFile pdf_file = new PDFFile(buffer);
PDFPage page = pdf_file.getPage(2, true);
RectF rect = new RectF(0, 0, (int) page.getBBox().width(),
(int) page.getBBox().height());
Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect);
FileOutputStream os = new FileOutputStream("/storage/extSdCard/pdf.jpg");
image.compress(Bitmap.CompressFormat.JPEG, 80, os);
//((ImageView) findViewById(R.id.testView)).setImageBitmap(image);
} catch (Exception e) {
e.printStackTrace();
}
You can change the rect around to make it extract any part of the pdf you want etc too, pretty good. Spent about 16 hours banging my head against a wall before finding that solution. Wasn't really sure if it was possible without the swing awt library. Sorry the storage is hard coded but it was the least of my concerns at the time.
I ended up finding out how to do what the question initially asked!!!
You need iTextG library (itextg-5.5.3.jar), scpkix-jdk15on.1.47.0.1.jar & scprov-jdk15on-1.47.0.2.jar
inside where want to call it from:
public static final String RESULT = "/storage/sdcard0/dirAtExtStorage/Img%s.%s";
public void extractImages(String filename)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(filename);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener(RESULT);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
}
}
inside MyImageRendererListener.java:
public class MyImageRenderListener implements RenderListener{
private String path;
public MyImageRenderListener(String path) {
this.path = path;
}
#Override
public void beginTextBlock() {
// TODO Auto-generated method stub
}
#Override
public void endTextBlock() {
// TODO Auto-generated method stub
}
public void renderImage(ImageRenderInfo renderInfo) {
try {
System.out.print("renderImage");
String filename;
FileOutputStream os;
PdfImageObject image = renderInfo.getImage();
if (image == null) return;
filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
os = new FileOutputStream(filename);
os.write(image.getImageAsBytes());
os.flush();
os.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
#Override
public void renderText(TextRenderInfo arg0) {
// TODO Auto-generated method stub
}
}
enjoy guys
The JPEG Images that ImageIO generated view correctly on windows file explorer, as well as safari webbrowser, but in FireFox, the resampled images are clipped.
How do I use ImageIO without corrupting the resamples?
The code should resize image keeping aspect ratio, as well as do jpeg compression, the convert it to a byte [] array, which could be written to a socket.
some of my code. in this snippet, I tried adding Jui library, but still the same issue.
public static BufferedImage imageistream;
public void Resample(String child,double width,double height) throws Exception, InvalidFileStructureException, InvalidImageIndexException, UnsupportedTypeException, MissingParameterException, WrongParameterException
{
String imagePath = "";
if(this.is_mac_unix == true)
{
imagePath = this.path+"/"+child;
}
else
{
imagePath = this.path+"\\"+child;
}
PixelImage bmp = null;
try {
bmp = ToolkitLoader.loadViaToolkitOrCodecs(imagePath, true, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Resample resample = new Resample();
resample.setInputImage(bmp);
double fixedRatio = width/height;
if(((double)bmp.getWidth()/bmp.getHeight()) >= fixedRatio)
{
resample.setSize((int)width,(int)(bmp.getHeight()/(bmp.getWidth()/width)));
}
else
{
resample.setSize((int)width,(int)(bmp.getWidth()/(bmp.getHeight()/height)));
}
resample.setFilter(Resample.FILTER_TYPE_LANCZOS3);
resample.process();
PixelImage scaledImage = resample.getOutputImage();
Processor.imageistream = ImageCreator.convertToAwtBufferedImage(scaledImage);
bmp = null;
Runtime rt = Runtime.getRuntime();
rt.gc();
}
...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(Processor.imageistream, "jpg", baos);
// ImageIO.write(Processor.imageistream, "png", baos); Works!
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte bytes[] = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
OutputStream os = (OutputStream)obj[1];
OutputStreamWriter writer = (OutputStreamWriter)obj[0];
byte[] buf= new byte[4096];
int c;
try {
while (true) {
c= is.read(buf);
if (c<= 0) break;
os.write(buf, 0, c);
}
writer.close();
os.close();
is.close();
I've been successfully using:
BufferedImage bufferedImage = ImageIO.read(..);
Image img = bufferedImage.getScaledInstance(..);
BufferedImage result = // transform Image to BufferedImage
ImageIO.write(result, "image/jpeg", response.getOutputStream());
transformation is simply writing the contents of the image to a new BufferedImage