Show images coming from mongodb into java swing - java

i'm a newbie in Java and Mongodb. Till now all works fine but now i complete stuck. I can write and retrieve images to and from Mongodb. But how i can show them on screen. I know i can get them from the folder but i like to show the image direct from database into, in this case because i learning, into swing.
i guess i need to convert them? OR how does it works?
Database dbb = new Database(mc, dba);
DB dbc1 = dbb.getDatabase("aatestdb");
String newFileName = "test foto";
GridFS gfsPhotoa = new GridFS(dbc1, "photofile");
GridFSDBFile imageForOutput = gfsPhotoa.findOne(newFileName);
System.out.println ("show i receive data");
// proof i got image from dbimageForOutput.writeTo("/Users/xyz/Pictures/foto_offshore_site/java_app.jpg");

Typically in Swing you can use a JLabel and ImageIcon to show an image. If you can get your GridFSDBFile into an InputStream, you could try ImageIO.read(InputStream) This may or may not work. If it doesn't work, you are going to have to get an ImageReader for the specific kind of image you are dealling with

Related

Is there any way to update the Exif tag in jpg file using java?

Im cleaning up my jpg files by tagging them in categories. However, the amount is too much to do it with hand operation so Im trying to tag the jpg files using java code (to automate it).
I googled several times but could not find the answer.
// What I want is something like this.
String path = "F:\\test.jpg"
File test_file = new File(path);
test_file.property.tag = "school";
test_file.property.tag = "xxxxCity";
None since I don't know the source code of doing it.

Is it possible to implement a command in CodenameOne that will display a PDF once a button is pressed without using a URL

I am trying to make a program using the CodenameOne plugin that will display various PDF's when buttons are pressed, but I can't find a way to do this without using a specific URL for each PDF. Is there any possible way to accomplish this by placing the PDF files into a package and calling them when needed? I would assume that I need to use an ActionListener but I really don't know what to do. This is what I have tried so far.
b1L1.addActionListener((e)->File file = new File("/path/to/file.pdf");
Desktop.getDesktop().open(file));
Desktop or other AWT related API's are unavailable. Even java.io.File doesn't make sense on Codename One due to mobile OS restrictions on file systems that java.io wasn't designed to handle see this for the long form explanation.
We have a sample that does exactly this taken from here:
Form hi = new Form("PDF Viewer", BoxLayout.y());
Button devGuide = new Button("Show PDF");
devGuide.addActionListener(e -> {
FileSystemStorage fs = FileSystemStorage.getInstance();
String fileName = fs.getAppHomePath() + "pdf-sample.pdf";
if(!fs.exists(fileName)) {
Util.downloadUrlToFile("http://www.polyu.edu.hk/iaee/files/pdf-sample.pdf", fileName, true);
}
Display.getInstance().execute(fileName);
});
hi.add(devGuide);
hi.show();

How to insert a data url/binary to replace an image in a libreoffice writer document?

I am creating a report printer in Java using the LibreOffice SDK and Apache Batik. Using Batik, I draw svgs which I then insert into a LibreOffice Writer document. To properly insert the image, all I found is using a path to load the image from disk and insert it into the document. So far so good, but I have to explicitly save the document to disk in order to read it into libreoffice again.
I tried to use a data url as the image path but it did not work. Are there any possibilities to read an image from a stream or anything else I can use without storing the file to disk?
I found a solution. I realized how to do it when I realized that all my images I added were just image links. So I had to embed the images instead.
To use this, you need:
Access to the XComponentContext
A TextGraphicObject in your document (see the links above)
The image as byte[] or use another stream
The code:
Object graphicProviderObject = xComponentContext.getServiceManager().createInstanceWithContext(
"com.sun.star.graphic.GraphicProvider",
xComponentContext);
XGraphicProvider xGraphicProvider = UnoRuntime.queryInterface(
XGraphicProvider.class, graphicProviderObject);
PropertyValue[] v = new PropertyValue[1];
v[0] = new PropertyValue();
v[0].Name = "InputStream";
v[0].Value = new ByteArrayToXInputStreamAdapter(imageAsByteArray);
XGraphic graphic = xGraphicProvider.queryGraphic(v);
if (graphic == null) {
LOGGER.error("Error loading the image");
return;
}
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, textGraphicObject);
// Set the image
xProps.setPropertyValue("Graphic", graphic);
This worked effortlessly even for my svg images.
Source: https://blog.oio.de/2010/05/14/embed-an-image-into-an-openoffice-org-writer-document/

Java viewing image path from MYSQL and displaying that image in java tips

I'm able to save an image selected with a JFileChooser to a BLOB column in MySQL in phpMyAdmin, but how can I view that BLOB and load it into a JFrame for display from within Java? Any code would be helpful.
Retrieve the BLOB from the database and create a BufferedImage using ImageIO.read
See here for how to paint the image.
The solution listed above by Dmitri will solve your problem, that is sure. I was use to do same think when i was novice programmer, but is not a good idea to store the image in Database at all in them of performance issues. Its better to store the image file location path into database and store the image in file system. This will save your lots of processing and improve your performance. For better understanding just read this wonderful discussion
Store image in database Vs File System.
This is a code segment that shows a picture in a JLabel object which is stored in DB in blob format .
Blob sqlphoto = (Blob) rs.getBlob("photo");
if (sqlphoto != null) {
InputStream photo = sqlphoto.getBinaryStream();
Image image = null;
try {
image = ImageIO.read(photo);
jLabel31.setIcon(new ImageIcon(image));
}
catch (IOException ex) {
Logger.getLogger(ModifyClerk.class.getName()).log(Level.SEVERE, null, ex);
}
}

Display Image from Server in Java

I am new in Java programming. My query is that I am having an image which is present on a server and I want to display that image inside the JFrame. I tried using the Image class but that seems to be not working.
Please Note: I don't want to use applets for this, so is there some other method by which this can be done?
Thanks & Regards,
Assuming that it's a public accessible webserver, you can use URL#openStream() to get an InputStream out of an URL.
InputStream input = new URL("http://example.com/image.png").openStream();
Then you can just create the BufferedImage with help of ImageIO#read() the usual way.
BufferedImage image = ImageIO.read(input);
// ...

Categories