Saving com.itextpdf.text.Image as a image file - java

Is there a way to save the com.itextpdf.text.Image file as a jpg file on the file system ?
Barcode39 code39 = new Barcode39();
code39.setCode(barcode);
code39.setStartStopText(false);
image39 = code39.createImageWithBarcode(cb, null, null);
image39.scaleAbsolute(width, height);
image39.setAbsolutePosition(top, left);
cb.addImage(image39);
I am creating a bar code image and adding it to a pdf. At the same time i want the image to be saved on the file system. Any help is appreciated.
OR,
Is it possible to retrieve the barcode from the pdf( both the barcode as well as the numbers under it) as an image file and save it to the file system using itext ?

Just convert the Barcode39 itext image into an AWT image using createAwtImage:
java.awt.Image awtImage = code39.createAwtImage(Color.BLACK, Color.WHITE);
Then convert it to a BufferedImage and store it:
BufferedImage bImage= new BufferedImage(awtImage.getWidth(), awtImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bImage.createGraphics();
g.drawImage(awtImage, 0, 0, null);
g.dispose();
ImageIO.write(bImage, "jpg", new File("code39.jpg"));

You can use this code:
BarcodeQRCode qrcode = new BarcodeQRCode("testo testo testo", 1, 1, null);
Image image = qrcode.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getWidth(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(image, 0, 0, null);
buffImg.getGraphics().dispose();
File file = new File("tmp.png");
ImageIO.write(buffImg, "png", file);
I hope it helps you.
Enrico

Related

How to convert 8-bit RGBA png image to 16 bpc RGBA png image using java 8?

I have used BufferedImage class to generate images from pdf. I am getting 8-bit RGBA by using the below-mentioned snippet, but I was unable to convert it to 16 bpc RGBA.
int page = 0;
BufferedImage bim = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_565_RGB);
bim = pdfRenderer.renderImage(page);
String fileName = OUTPUT_DIR + "image-" + page + ".png";
ImageIOUtil.writeImage(bim, fileName, 1);
To create a 16 bit per sample (or channel) BufferedImage and store it as a PNG, you can use the following code:
ComponentColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_USHORT);
BufferedImage image = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(100, 100), colorModel.isAlphaPremultiplied(), null);
if (!ImageIO.write(image, "PNG", new File("test.png"))) {
System.err.println("Could not write PNG: " + image);
}
From the code, I'm assuming you are using PDFBox. If you are using PDFBox, you can use ImageIOUtil.writeImage instead of ImageIO.write. But you probably want to set the DPI to something more reasonable than 1... 😉
Also note that image = pdfRenderer.renderImage(page) in your code will replace the image, so you can't use that. Try using the renderPageToGraphics(int, Graphics2D)method instead:
ComponentColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_USHORT);
BufferedImage image = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(100, 100), colorModel.isAlphaPremultiplied(), null);
Graphics2D g = image.createGraphics();
try {
pdfRenderer.renderPageToGraphics(page, g);
}
finally {
g.dispose();
}
if (!ImageIOUtil.writeImage(image, test.png, 72))) {
System.err.println("Could not write PNG: " + image);
}

Save a BufferedImage to BMP/PNG/JPG and then open it back

I have made a project like Paint from windows and now I want to make the save/open buttons. I have found out how to save the bufferedImage, but the problem is how do I open it back in the correct location and be able to draw on it again?
To read in an image, use ImageIO.
File myPath = new FIle("path to image");
BUfferedImage img = ImageIO.read(myPath);
Also what you could (should) do is load the image into your user space so that you don't edit the original image:
public static BufferedImage userSpace(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = newImage.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose();
return newImage;
}

Export PowerPoint slides into java.awt.Graphics2D

i started a test to convert a ppt document to jpeg or png image. i use java to test.
follow the instruction on the apache web: http://poi.apache.org/slideshow/how-to-shapes.html#Render, and the code:
FileInputStream is = new FileInputStream("slideshow.ppt");
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide[i].draw(graphics);
//save the output
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}
in the ppt doc, i just type the "hello world", than i run the java program, and the png image generated successfully. but i open the image with ACDsee software to view the image, but the "helloworld" didn't appear in the png image. what is the matter? Can anyone here give me some advice? you can also test by yourself to look at the result, pls let me know if you have got the same result.
It appears that you aren't doing anything at all with your BufferedImage. You are simply filling the image with white and saving it to a file.

Is it possible to create image programmatically on Java, Android?

I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?
It's definately possible.
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
I used this tutorial to learn the basics of the canvas and paint.
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
Yes, see here
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
You can also use awt's Graphics2D with this compatibility project
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}

Resizing a RenderedImage

I'm using a RenderedImage to display tiffs in a DisplayJAI in my app.
Somebody know how to resize an instance of RenderedImage??
It's been a long time since I've done anything with JAI, but wouldn't the "Scale" or "Affine" operations suffice?
Edit: Here are a couple of links into the Programmer's Guide for "Scale" and "Affine".
public static RenderedImage scale(RenderedImage image, float scaleFactor)
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
RenderedOp resizeOp = SubsampleAverageDescriptor.create(image,
Double.valueOf(scaleFactor), Double.valueOf(scaleFactor), hints);
BufferedImage bufferedResizedImage = resizeOp.getAsBufferedImage();
return bufferedResizedImage;
}
There is an example code posted here to just that:
http://answers.yahoo.com/question/index?qid=20090827075608AA12kEZ
Relevant code:
BufferedImage img = ImageIO.read(new File("~/your/file/system/example.jpeg"));
BufferedImage thumb = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_RGB);
thumb.createGraphics().drawImage(
img.getScaledInstance(w2, h2, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
File file = new File(fullpath + filename);
ImageIO.write(thumb, "png", file);

Categories

Resources