Two Java image processing component Jmagick instance

Source: Internet
Author: User
Tags imagemagick

Download the address: http://downloads.jmagick.org/

Download the installation Imagemagick-6.3.9-0-q16-windows-dll.exe first, and copy all DLL files under the installation directory (found by the directory you installed) to the "C:windowssystem32" folder under the system disk.

then download the corresponding Jmagick jmagick-win-6.3.9-q16.zip decompression jmagick.dll Copy to the "C:windowssystem32" folder under the system disk; Jmagick.jar copied to the Lib folder under Tomcat or the Web-inf lib file of the item being used

Then proportional scaling

public static void Changeimge (file img, int width, int height) {

try {

Image image = Imageio.read (IMG);

Size of the picture, if the length is smaller than the specified size, then return, if there is a greater than the specified size, proportional scaling

int srch = Image.getheight (null);

int SRCW = Image.getwidth (null);

if (srch <= height && srcw <= width) {

Return

}

int tmph = width;

int TMPW = height;

Limited in length and width, not exceeding set value

while (Srch > Height | | srcw > width) {

if (SRCW > width) {

tmph = Srch * WIDTH/SRCW;

Srch = tmph;

Srcw=width;

}

if (Srch > height) {

TMPW = SRCW * Height/srch;

SRCW = TMPW;

Srch=height;

}

}

BufferedImage bufferedimage = new BufferedImage (SRCW, Srch,

BUFFEREDIMAGE.TYPE_3BYTE_BGR);

Bufferedimage.getgraphics (). DrawImage (

Image.getscaledinstance (SRCW, Srch, Image.scale_smooth), 0,

0, SRCW, srch, NULL);

FileOutputStream fos = new FileOutputStream (IMG);

JPEGImageEncoder encoder = jpegcodec.createjpegencoder (FOS);

Encoder.encode (BufferedImage);

Fos.close ();

System.out.println ("Conversion succeeded ...");

catch (IOException e) {

E.printstacktrace ();

throw new IllegalStateException ("Picture conversion error!") ", e);

}

}

Code two
Java code

Import java.awt.dimension;

Import Java.awt.rectangle;

Import Java.text.simpledateformat;

Import Java.util.date;

Import Magick.compositeoperator;

Import Magick.compressiontype;

Import Magick.drawinfo;

Import Magick.imageinfo;

Import magick.magickexception;

Import Magick.magickimage;

Import Magick.pixelpacket;

Import Magick.previewtype;

public class Imageutils {

static{

You can't miss this, or Jmagick.jar's path can't be found.

System.setproperty ("Jmagick.systemclassloader", "no");

}

See a use Jmagick to generate thumbnails

Import java.awt.dimension;
Import Java.awt.rectangle;
Import Java.awt.image.bufferedimage;
Import Java.io.file;
Import Java.text.simpledateformat;
Import Java.util.date;
Import Javax.imageio.imageio;
Import Magick.compositeoperator;
Import Magick.compressiontype;
Import Magick.drawinfo;
Import Magick.imageinfo;
Import magick.magickexception;
Import Magick.magickimage;
Import Magick.pixelpacket;
Import Magick.previewtype;
public class Imageutil2 {
static {
You can't miss this, or Jmagick.jar's path can't be found.
System.setproperty ("Jmagick.systemclassloader", "no");
}
/**
* Compress Pictures
*
* @param filepath source file path
* @param topath thumbnail path
*/
public void CreateImage (string filepath,string topath, int width, int height) {
Imageinfo info = null;
Magickimage image = null;
Magickimage scaled = null;
try {
info = new Imageinfo (filepath);

Image = new Magickimage (info);
scaled = image.scaleimage (width, height);//small picture file size.
Scaled.setfilename (Rootpath+topath);
Scaled.writeimage (info);
catch (Exception e) {
E.printstacktrace ();
finally {
if (image!=null)
{
Image.destroyimages ();
}
if (scaled!= null) {
Scaled.destroyimages ();
}
}
}
public static void Main (string[] args) {
CreateImage ("D:1012.jpg", "d:www.111cn.net_th.jpg", 110,100);
System.out.println (Isupdatebigpic ("d:www.111cn.net11.jpg"));
}
}

ImageMagick is a powerful, stable, and free toolset and development kit that can be used to read, write, and process picture files in more than 89 basic formats, including popular TIFF, JPEG, GIF, PNG, PDF, and PHOTOCD formats. With ImageMagick, you can dynamically generate pictures based on the needs of your Web application, and you can resize, rotate, sharpen, subtractive, or add special effects to one (or a group) of pictures and save the results of the operation in the same format or in other formats. Operations on pictures can be done either through the command line or by programming in C/E + +, Perl, Java, php tutorials, python, or Ruby. At the same time, ImageMagick provides a high-quality 2d toolkit that partially supports SVG. Now, Imagemagic's main focus is on performance, reducing bugs, and providing stable APIs and ABI

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.