Java 8 Implementation Picture BASE64 codec _java

Source: Internet
Author: User
Tags base64 readfile string format

Objective

The basic encoding is a standard BASE64 encoding for handling conventional requirements: the output does not add line breaks, and the output is composed of letters plus numbers.

Recently made a Web template, which wants to use the Base64 background image. Although the network has a ready-made encoder, but always want to implement one of their own. As many people may not know, the new Base64 class provided by JDK 8 can handle this task very easily: Base64 (Java Platform SE 8).

First, choose a picture

Mm.png

Second, the establishment of HTML demo document template

Test.html

<! DOCTYPE html>
 
 

Note url() that we are going to embed the generated BASE64 encoded string into the space later.

Iii. Writing Java programs

A bidirectional implementation of BASE64 codec is implemented, although in this case only the Base64 decoding is needed. The annotations to the Java program are clear, directly on the code:

' Base64trans.java ' package com.gua;
 /** * Created by 2gua on 2014/10/5.
* * Import java.io.IOException;
Import java.nio.file.*;

Import java.util.Base64;

Import static java.lang.System.out;
  The public class Base64trans {/** * reads content from the picture file.
  * @param path to the path picture file.
  * @return A byte array of binary picture contents.

  * */Private byte[] readFile (path Path) {byte[] imagecontents = null;
  try {imagecontents = files.readallbytes (path);
  catch (IOException e) {out.println ("Error reading file ... ~zz");
 return imagecontents;
  /** * encoded picture file, encoded content output to {@code String} format.
  * @param a byte array of the contents of the imagecontents binary picture.
  * @return The encoded content in the {@code String} format. * * Private String base64encoding (byte[] imagecontents) {if (imagecontents!= null) return Base64.getencoder (). Encode
  ToString (imagecontents);
 else return null;
  /** * decode picture file.
  * @param imagecontents The string format of the picture file to be decoded.
  * @return The binary content of the image file after decoding. * Private byte[] base64decoding (String imagecontents) {if (imagecontents!= null) return BasE64.getdecoder (). Decode (imagecontents);
 else return null;
  /** * Writes the decoded binary content to the file.
  * @param path to write to.
  * @param the binary content of the decoded imagecontents. * * private void WriteFile (path path, byte[] imagecontents) {if (imagecontents!= null) try {files.write (path, I
   Magecontents, standardopenoption.create);
   catch (IOException e) {out.println ("Error writing file ... ~zz");
  } public static void Main (string[] args) {Base64trans bt = new Base64trans ();

  String encodingstring = bt.base64encoding (Bt.readfile (Paths.get ("d:/temp/mm.png"));

  Out.println ("Binary picture file Base64 code:" + encodingstring);

  Bt.writefile (Paths.get ("D:/temp/mm2.png"), Bt.base64decoding (encodingstring));
 OUT.PRINTLN ("Task over ..."); }
}

The results of the program are as follows:

Binary picture file Base64 code: IVBORW0KGGOAAAANSUHEUGAAAIAAAACACAYAAADDPMHLAAAABGDBTUEAALGPC/XHBQAAACB
... (n multiple characters omitted here)
8xntiwl2vhc3lpy29ulmnul2vhc3lpy29ulmnul2nkbi1pbwcuzwfzewljb24uy24vcg5nlzewodcwlzewodcwodyucg5nplffdwaaaabjru5erkjggg = =
Task End ...

Four, the final setup

To reference BASE64 encoded content in HTML file template test.html, there is one more step to take: Add the following to the beginning of the BASE64 encoded content:data:image/png;base64,

The end result is something like this:

DATA:IMAGE/PNG;BASE64,IVBORW0KGGOAAAANSUHEUGAAAIAAAACACAYAAADDPMHLAAAABGDBTUEAALGPC/XHBQAAACB ...
(n multiple characters omitted here)
8xntiwl2vhc3lpy29ulmnul2vhc3lpy29ulmnul2nkbi1pbwcuzwfzewljb24uy24vcg5nlzewodcwlzewodcwodyucg5nplffdwaaaabjru5erkjggg ==

Then, insert this long string of strings into parentheses in the following line of code url() :

Background:url () No-repeat Center Center;

If the picture is in jpg, change the data:image/png;base64 png above to JPEG, or GIF if it is in GIF format. OK, refresh the page and see the results:

There are circle circles because we set up some CSS3 effects.

We also implemented the BASE64 decoding, and generated a new mm2.png picture, in the image browser open mm2.png, the effect should be the same as mm.png.

In fact, every time the JDK release, there are many big and small updates, but whether you intend to explore it. Java is always so robust and reassuring.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.