I am generating QR image, and then I save it in PDF file. I am using the following code:
BarcodeQRCode qrcode = new BarcodeQRCode("This is a test QR code!", 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);
File qrImageFile = new File(qrImageFilePath);
ImageIO.write(buffImg, "png", qrImageFile);
Because my PDF file is based on HTML string, so I put the QR image file path in the string (besides other texts and images), and then write the whole string in the PDF.
My question: Can I do the same process without saving the Image on my computer? And if yes, how I can include it in the string?
Thank you.
Related
I wish to create a new user programmatically and giving this user a portrait. I'm using a csv file, to loop through my code, which has a number of entries. Their fullname, their email adres and a url which links to an image is all in this csv file.
The users are made but they don't get a profile picture, however if i use a path to my local machine it works just fine. Is there a way i can use a url and save the image as an object in java?
My code so far using a local file:
// Getting image from local path
File sourceImage = new File("C:\\Users\\path_to_image");
BufferedImage img = ImageIO.read(sourceImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
byte[] bytes = baos.toByteArray();
// Adding profile picture to newly made user
UserLocalServiceUtil.updatePortrait(user.getUserId(), bytes);
I do not want to have to download the pictures because there are quite a lot of them, but if that is the only way I'll have to of course.
Any help would be much appreciated!
I am using the following code:
OutputStream outputfile = new FileOutputStream("resource/LeftIndexSegmentMy.WSQ");
ImageIO.write(subImage,"WSQ", outputfile);
However, the resulting file is empty. No image is there.
So, how do I write an image in the WSQ format?
I have an int array of color r,g and b values. And I would like to encode them in a image file. Is there an easy method in android to write this data to an image? Also which image format should I use for this, png?
Create a bitmap using your int array like this using Bitmap.createBitmap:
int[] array; // array of int RGB values e.g. 0x00ff0000 = red
Bitmap bitmap = Bitmap.createBitmap(array, width, height, Bitmap.Config.ARGB_8888);
Then write it out using Bitmap.compress:
outStream = new FileOutputStream(filepath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
You can call Environment.getExternalStorageDirectory() to get a folder on external storage where you can save the file, if that's where you want to save it. You can get the path with get File.getAbsolutePath(), e.g:
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/image.png";
You need the WRITE_EXTERNAL_STORAGE permission defined in your AndroidManifest.xml to be able to write to files on external storage.
There are pure java implementations for reading and writting images:
Image processing library for Android and Java
Probably some will work in android out of the box
I believe ImageIO is available in Android. The ImageIO API provides methods to read the source image and to write the image in the new file format.
To read the image, simply provide the ImageIO.read() method a File object for the source image. This will return a BufferedImage.
//Create file for the source
File input = new File("c:/temp/image.bmp");
//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);
Once you have the BufferedImage, you can write the image as a PNG. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "png".
//Create a file for the output
File output = new File("c:/temp/image.png");
//Write the image to the destination as a PNG
ImageIO.write(image, "png", output);
I am Currently using ICEPDF to render PDF files and display it in my java Swing application (in Internal Frame). Now I want to add crop features to my Java application. Like, if I click a button, I can drag required portion of PDF and save it as Image in my local storage.
Is there an efficient way to crop out PDF and save as image (.JPG) via java program?
Ghost4J library (http://ghost4j.sourceforge.net), is your best option:
3 simple step:
Load PDF files:
PDFDocument document = new PDFDocument();
document.load(new File("test.pdf"));
Create the renderer:
SimpleRenderer renderer = new SimpleRenderer();
// set resolution (in DPI)
renderer.setResolution(600);
Render:
List<Image> images = renderer.render(document);
Then you can do what you want with your image objects, for example, you can write them as JPG like this:
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "jpg", new File((i + 1) + ".jpg"));
}
Ghost4J uses the native Ghostscript API so you need to have a Ghostscript installed.
EDIT: investigating a bit, if you convert the PDF to Image you won't have much problem to crop them:
BufferedImage is a(n) Image, so the implicit cast that you're doing in the second line is able to be compiled directly. If you knew an Image was really a BufferedImage, you would have to cast it explicitly like so:
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
Then you can crop it with BufferedImage::getSubimage method:
private BufferedImage cropImage(BufferedImage src, Rectangle rect) {
BufferedImage dest = src.getSubimage(rect.x, rect.y, rect.width, rect.height);
return dest;
}
I'm trying to scan an image and save it to a file given a specific format (Tiff or Jpeg) with a Swing application, using Morena and Sane.
I load the whole image in memory with this process:
SaneSource source = /* source implemented here */;
MorenaImage morenaImage = new MorenaImage(source);
Image image=Toolkit.getDefaultToolkit().createImage(morenaImage);
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimg.createGraphics();
g.drawImage(image, 0, 0, null);
ImageIO.write(bimg, "jpg", new File(filename));
I'm pretty sure there is a better way to do this without eating all my memory, like streaming the content of my scanned image in cache to the file with a Consumer / Observer, but I couldn't wrap my mind good enough around these notions to create my own solution.
Could you please help me down the path to better image processing?
Thanks in advance, david
You should attach an ImageConsumer (that will write image to OutputStream using your favorite image format) directly to an ImageProducer (SaneSource or MorenaImage if you wish). You can find ImageConsumer example that encodes image as PPM and transfers it to OutputStream here. You'll need to write something like this to use this example:
ImageProducer prod = ... your producer here ....;
PpmEncoder ppm = new PpmEncoder(prod, myOutputStream);
ppm.encode();