Java Picture Conversion Tool

Source: Internet
Author: User
Tags base64

 Packagecom.sicdt.sicsign.web.utils;ImportJava.awt.image.BufferedImage;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJavax.imageio.ImageIO;Importorg.apache.commons.io.FileUtils;Importcom.itextpdf.text.Document;Importcom.itextpdf.text.DocumentException;ImportCom.itextpdf.text.Image;Importcom.itextpdf.text.PageSize;ImportCom.itextpdf.text.pdf.PdfWriter; Public classMyimgutil {/*** <br> Description: Convert picture BASE64 string to binary array * <br> Author: seven Pulse *@parambase64 New Image (); Img.src or Canvas.todataurl ("Image/png") *@return     * @throwsIOException*/     Public Static byte[] imgbase64tobytes (String base64) {Sun.misc.BASE64Decoder decoder=NewSun.misc.BASE64Decoder (); //because the parameter base64 source is different, the attachment data replacement needs to be emptied out. If this entry comes from Canvas.todataurl ("Image/png");Base64 = Base64.replaceall ("Data:image/png;base64,", "" "); //base64 decoding and converting to a binary array        byte[] bytes =NULL; Try{bytes=Decoder.decodebuffer (base64);  for(inti = 0; i < bytes.length; ++i) {if(Bytes[i] < 0) {//Adjust Exception DataBytes[i] + = 256; }            }        } Catch(IOException e) {e.printstacktrace (); }        returnbytes; }        /*** <br> Description: Image/File to binary array, this method has many, write only one * <br> Author: seven Pulse *@paramimgpath Picture Path *@return     * @throwsFileNotFoundException*/     Public Static byte[] imgtobytes (String imgpath) {File file=NewFile (Imgpath); BufferedImage Bi=NULL; Bytearrayoutputstream BAOs=NULL; Try {            //files using other toolsBI =imageio.read (file); BAOs=NewBytearrayoutputstream (); intindex = Imgpath.lastindexof (".")); String format= Imgpath.substring (index+1);            Imageio.write (BI, format, BAOs); byte[] bytes =Baos.tobytearray ();            Baos.close (); returnbytes; } Catch(IOException e) {e.printstacktrace (); }        return NULL; }        /*** <br> Description: Convert binary to Picture * <br> Author: seven Pulse *@paramOutpath where to export the picture *@paramSavepath Save Location*/     Public Static voidBytestoimg (byte[] bytes,string Savepath) {Bytearrayinputstream BAOs=Newbytearrayinputstream (bytes); Try{bufferedimage bi=Imageio.read (BAOs); File File=NewFile (Savepath); Imageio.write (BI,"PNG", file);        Baos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }        /*** <br> Description: Picture binary array to PDF binary array * <br> Author: seven Pulse *@paramimgbytes Binary array of images *@return     */     Public Static byte[] Imgbytestopdfbytes (byte[] imgbytes) {            byte[] bytes =NULL; Document Document=NewDocument (); Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Try{pdfwriter.getinstance (document, BAOs); //set the size of a documentdocument.setpagesize (PAGESIZE.A4); //Open DocumentDocument.open (); //read a pictureImage image =image.getinstance (imgbytes); floatImageWidth =image.getscaledwidth (); inti = 0;  while(ImageWidth > 600) {image.scalepercent (100-i); I++; ImageWidth=image.getscaledwidth ();                } image.setalignment (Image.align_center); //Insert a pictureDocument.add (image); //go to binary arraybytes =Baos.tobytearray (); returnbytes; } Catch(documentexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally {                if(NULL!=BAOs) {                    Try{baos.close (); } Catch(IOException e) {e.printstacktrace (); }                }                if(NULL!=document)                {Document.close (); }            }            returnbytes; }            /*** <br> Description: Binary to file, what binary to what kind of file * <br> Author: seven Pulse *@parambytes Binary array *@paramSavepath File Save path*/     Public Static voidBytearraytofile (byte[] bytes,string Savepath) {        Try{fileutils.writebytearraytofile (NewFile (Savepath), bytes); } Catch(IOException e) {e.printstacktrace (); }    }         Public Static voidMain (string[] args)throwsIOException {//bytestoimg (Imgtobyte ("C://Users//Administrator//Desktop//test.png ")," C://Users//Administrator//Desktop//bytes2img.png ");Bytearraytofile (Imgtobyte ("C://users//administrator//desktop//test.png"), "c://users//administrator//desktop// Test2.png "); }}

Java Picture Conversion Tool

Related Article

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.