Reading, modifying, and saving images

Source: Internet
Author: User
By Julian Robichaux,
Snapps

This month's developer article talks about how to manipulate image files
(Like jpg or PNG files) with your code. While there are a number of third-party
DLLs and applications you might tap into for this purpose, you'll probably find
That you can do most, if not all, of the things you want to do using Java right
Out of the box.

Reading Images
The built-in Java
Library for reading and writing images is the ImageIO library. This was
Available as an extra library in early versions of Java (you cocould download it
From sun and add it to your classpath), and it has been a part of the standard
Java distribution since Java 1.4. That's Lotus Notes 7.0 and higher to you and
Me.

As Java versions have been updated with newer
Versions of the Notes client, we have also gained some additional functionality
With the built-in ImageIO Framework with newer versions of the client.
Specifically, here are the image types we can read in notes 7 and higher:

    • Notes 7: JPG and PNG
    • Notes 8: JPG, PNG, BMP, and GIF
    • Notes 8.5: JPG, PNG, BMP, and GIF

An interesting thing to keep in mind is that while notes 8 canReadGIF files, supportWritingGIF files (as we'll see later) is not
Available by default until notes 8.5. Also, the ImageIO Framework has an API
Plugins, so there are additional plugins available that you can use to read
And/or write other image formats.

I 've also found that certain types of PNG files are not able to be read
Properly by the base Java ImageIO class. See this page for one instance and a little explanation. As
The use of additional file types, you might find that you have to use other
Libraries or plugins (like javapng from Google)
To get full PNG support.

Reading a file for manipulation asBufferedimageIs as easy as writing a few lines of code
Like this:

 
Fileinputstream FCM = new fileinputstream (filename); bufferedimage Bi = ImageIO. Read (FCM );

From there, you can modifyBufferedimageTo
Make any alterations to the image before resaving it.

Modifying Images
Modifying an image
Involves making modifications to the in-memoryBufferedimageThat we now have access to. There are two
Primary ways in which we will modify an image: redrawing the image and filtering
The image.

Redrawing an image (cropping it, rotating it,
Adjusting its size) can be done by using the graphics2d object to create a new
"Canvas" to redraw the bufferedimage onto it, thereby creating a new
Bufferedimage at the desired size, rotation, or position. For example, here's
How to resize an image:

 
Bufferedimage bi2 = new bufferedimage (width, height, Bi. getType (); graphics2d g2 = bi2.creategraphics (); g2.setrenderinghint (renderinghints. key_interpolation, hint); g2.drawimage (Bi, 0, 0, width, height, null); g2.dispose (); Return bi2;

You can callG2.rotate ()Before the callG2.drawimage ()If you want to rotate the image, and you
Can actually crop the image by callingBufferedimage. getsubimage ()To create a new croppedBufferedimage.

You can actually use affinetransforms (which we'll discuss in the next
Paragraph) to do the image scaling or rotation as well. However, you have much
Better control over the quality of the resulting image if you use graphics2d
Methods directly for this functionality. For a good workaround for the problem
Of poor image quality when you create image thumbnails, see Chris Campbell's
Article "the perils of image. getscaledinstance ()".

Pretty much any other kind of image manipulation
You want to do-like adjusting colors and contrast, blurring, sharpening, etc.
-Requires the use of a Java 2D bufferedimageop filter. Some of the ones you'll
Find useful are:

    • AffinetransformopFor
      Scaling, flipping, or rotating (although as mentioned above, you might get
      Better results for scaling and rotating by using graphics2d directly)
    • ColorconvertopFor
      Adjusting Image colors or converting to greyscale
    • RescaleopFor
      Adjusting contrast and brightness
    • ConvolveopFor
      Blurring, sharpening, or edge-detection

Here's an example of creating a greyscale version
Of an image:

 
Colorconvertop op = new colorconvertop (colorspace. getinstance (colorspace. cs_gray), null); Return op. Filter (Bi, null );

None of these operations will directly modifyBufferedimageThat they operate on; rather, they do their
Work and return a new instance ofBufferedimage
That you must now retain in memory.

Saving
Images
TheImageIOClass again comes
Play when we saveBufferedimageThat we 've
Modified. We just have to make a callImageIO. Write ()To save the in-memory image to a file.
Example:

 
String filename = "C :\\ mynewfile.png"; ImageIO. Write (Bi, "PNG", new file (filename ));

As with the file types that can be read, the file
Types that we can generate are different for different versions of Lotus
Notes:

    • Notes 7: JPG and PNG
    • Notes 8: JPG, PNG, and BMP
    • Notes 8.5: JPG, PNG, BMP, and GIF

Even if you don't make any changes to the image
File at all, you can useImageIO classTo
Convert from one file type to another. One limitation that I 've run into is that
The JPG output quality is often pretty poor. I don't know why that is, but
Workaround I 've found is to use code like this for generating JPG output:

Iterator iter = ImageIO. getimagewritersbyformatname ("Jpeg"); imagewriter writer = (imagewriter) ITER. next (); imagewriteparam iwp = writer. getdefaultwriteparam (); iwp. setcompressionmode (imagewriteparam. mode_explicit); iwp. setcompressionquality (quality); // 1f = 100%, 0.9f = 90%, etc. file file = new file (filename); fileimageoutputstream out = new fileimageoutputstream (File); writer. setoutput (out); iioimage image = new iioimage (Bi, null, null); writer. write (null, image, iwp); out. close (); writer. dispose ();

As I mentioned in the "reading images" section, there are plugins and
Additional libraries available that can be used to generate other image types
Too.

What about LotusScript?
If you're
LotusScript programmer, you can still take advantage of all this built-in
Functionality directly through your Lotus script code. Just use ls2j. In fact,
Example of using standards of the techniques I 've talked about abve as well as
Ls2j wrapper to make cballs to an image manipulation Java library is available on
My
Web site.

Limitations
The primary (and
Obvious) limitation is the small number of file types that you are limited.
However, that's just an "out of the box" limitation that you can use plugins and
Other libraries to work around. Ask Google for help.

If you're doing a large amount of Image Processing (or working on very large
Images), make sure you don't hold too recordsBufferedimagesIn memory.

Also, because you will almost certainly be dealing with the local file system
(Reading files, writing files, or both), make sure that any server-based agents
That use these techniques have the security set to "allow restricted operations"
And the agent signer is allowed to run unrestricted operations on
Server.

Bonus:
Embedding images into rich text fields
As a related topic, see my
December 2008 clippings article "four things you thought you couldn't do with Rich Text" to get
Some information on how you can embed an image file directly into a Rich Text
Field-as a real image, not just an attachment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.