Picture byte transform file format checksum

Source: Internet
Author: User
Import Java.awt.image.BufferedImage;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;


Import Javax.imageio.ImageIO;


public class Test {




public static void Main (string[] args) throws Exception {
Picture bytes
byte[] bytes = imagetobytes ("e:/jianchu.jpg", "jpg");
System.out.println (bytes.length);
byte turn picture
Bytearrayinputstream ImageStream = new Bytearrayinputstream (bytes);
BufferedImage image = Imageio.read (ImageStream);
BufferedImage image=imageio.read ("e:/xu woman. jpg");//
Imageio.write (image, "JPG", New File ("Src/test/resources/bb.jpg"))//can be relative or absolute path
Checksum format
Test T = new Test ();
System.out.println (t.verifyrtf ("JPG", "a.jpg"));
Pattern pattern = Pattern.compile (". *\\. i) JPG ");
Matcher Matcher = Pattern.matcher ("a.jpg");
Boolean B = matcher.matches ();
System.out.println (b);


}//bmp, PNG, JPEG, JPG, GIF

public static byte[] Imagetobytes (String ImagePath, String pictrtf) throws IOException {
BufferedImage buffer = imageio.read (new File (ImagePath));
Bytearrayoutputstream ImageStream = new Bytearrayoutputstream ();
Imageio.write (buffer, pictrtf, imagestream);
Imagestream.flush ();
byte[] bytes = Imagestream.tobytearray ();
return bytes;
}
/**
* Verify Format
* @param filertf
* @param filefullname
* @return
*/
public boolean verifyrtf (String filertf, String filefullname) {
Return Pattern.compile (". *\\.") i) "+ filertf). Matcher (Filefullname). matches ();
}

/**
* Imagetobytes
*
* @param bytes
* @throws IOException
*/
public void bytestoimage (byte[] bytes) throws IOException {
File Apple = new file ("E:/tes2.jpg");//write a picture of a byte array to another place
FileOutputStream fos = new FileOutputStream (apple);
Fos.write (bytes);
Fos.flush ();
Fos.close ();
}


/**
* Imagetobytes
*
* @param ImagePath
* @return
*/
Public byte[] Imagetobytes (String imagepath) {
FileInputStream fs = null;
try {
FS = new FileInputStream (ImagePath);
catch (FileNotFoundException e) {
E.printstacktrace ();
}
byte[] bytes = NULL;
try {
bytes = new byte[fs.available ()];
Fs.read (bytes);
Fs.close ();
catch (IOException e) {
E.printstacktrace ();
}
return bytes;
}


}

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.