ASCII Art Java example

A funny Java example to create an ASCII art graphic. The concept is simple, get the image’s rgb color in “integer mode”, later, replace the color’s integer with ascii text. P.S This example is credited for this post ASCIIArtService.java package com.mkyong.service; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; public class ASCIIArtService { public static void main(String[] …

Read more

Android ImageView example

In Android, you can use “android.widget.ImageView” class to display an image file. Image file is easy to use but hard to master, because of the various screen and dpi in Android devices. Note Please refer to this official Android’s “Drawable Resource” and “Screen Support” article for better understand of how image works in Android. In …

Read more

Download image file from JAX-RS

In JAX-RS, for user to download an image file, annotate the method with @Produces(“image/image-type”) : Put @Produces(“image/png”) on service method, for “png” image. Set “Content-Disposition” in Response header to prompt a download box. Note For other image types, refer to this list of the image types 1. Download Image in JAX-RS Full example to download …

Read more

Java MongoDB : Save image example

In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS APIs are able to serve other binary files as well, like video and music files. Note For detail explanation, read this MongoDB GridFS manual. 1. Save image Code snippets to save an image file into MongoDB, …

Read more

JSF 2 graphicImage example

In JSF, you can use <h:graphicImage /> tag to render a HTML “img” element. For example, an image named “sofa.png” in a resources folder, see figure below : 1. JSF 1.x graphicImage In JSF 1.x you can hard-coded above image URL directly in the “value” attribute : JSF… <h:graphicImage value="resources/images/sofa.png" /> HTML output… <img src="resources/images/sofa.png;" …

Read more

How to check if an image is loaded with jQuery

To check if an image is loaded successful or not, you can combine the use of jQuery ‘load()‘ and ‘error()‘ event : $(‘#image1’) .load(function(){ $(‘#result1’).text(‘Image is loaded!’); }) .error(function(){ $(‘#result1’).text(‘Image is not loaded!’); }); If the image is loaded successful, the load() function is called, otherwise the error() function is called. Try it yourself <html> …

Read more

Hibernate – save image into database

To save an image into database, you need to define a table column as blob data type in MySQL, or equivalent binary type in others database. In Hibernate side, you can declare a byte array variable to store the image data. Download this example – Hibernate-Image-Example.zip Here’s an Maven project to use Hibernate to save …

Read more

How to read an image from file or URL

The “javax.imageio” package is used to deal with the Java image stuff. Here’s two “ImageIO” code snippet to read an image file. 1. Read from local file File sourceimage = new File("c:\\mypic.jpg"); Image image = ImageIO.read(sourceimage); 2. Read from URL URL url = new URL("https://mkyong.com/image/mypic.jpg"); Image image = ImageIO.read(url); ImageIO Example In this example, you …

Read more

How to read and write an image in Java

In Java, we can use the javax.imageio.ImageIO class to read and write an image. 1. Read an image Read an image from a file. BufferedImage image = ImageIO.read(new File("c:\\test\\image.png")); Read an image from an URL. BufferedImage image = ImageIO.read(new URL("https://example.com/image.png")); 2. Write or save an image Write or save an image in different image formats. …

Read more

How to validate image file extension with regular expression

Image File Extension Regular Expression Pattern ([^\s]+(\.(?i)(jpg|png|gif|bmp))$) Description ( #Start of the group #1 [^\s]+ # must contains one or more anything (except white space) ( # start of the group #2 \. # follow by a dot "." (?i) # ignore the case sensive checking for the following characters ( # start of the …

Read more

How to convert byte[] to BufferedImage in Java

This article shows how to convert a byte[] to a BufferedImage in Java. InputStream is = new ByteArrayInputStream(bytes); BufferedImage bi = ImageIO.read(is); The idea is puts the byte[] into an ByteArrayInputStream object, and we can use ImageIO.read to convert it to a BufferedImage. 1. Convert byte[] to BufferedImage. The below example shows how to convert …

Read more

How to convert BufferedImage to byte[] in Java

This article shows how to convert a BufferedImage to a byte array or byte[]. BufferedImage bi = ImageIO.read(new File("c:\\image\\mypic.jpg")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", baos); byte[] bytes = baos.toByteArray(); The idea is uses the ImageIO.write to write the BufferedImage object into a ByteArrayOutputStream object, and we can get the byte[] from the ByteArrayOutputStream. …

Read more