根据pda传入矢量图的base64,还原成适量图



根据pda传入矢量图的base64,还原成适量图
package com.ibm.bgs.util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.ibm.bgs.test.JeruGraphics;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64 {


private BufferedImage image;

/**
* 创建jpg文件到指定路径下
* @param path
*/
public void createJpg(String path) {
try{
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}catch(FileNotFoundException fnfe) {
System.out.println(fnfe);
}catch(IOException ioe) {
System.out.println(ioe);
}
}

/**
* 根据矢量图的base64码生成byte,格式化标准:每位4个长度,为负正
* @param strBase64 :适量图坐标位的BASE64
* @return
*/
public String getByteFromBase64(String strBase64){
//String strBase64="CQAAAHNpZ25hdHVyZfAAAACTAAAAAgAAAAoAAAAFAAAABAAAAAkAAAAGAAAADwAAAAsAAAATAAAADwAAABcAAAASAAAAGAAAABYAAAAaAAAAGgAAABsAAAAeAAAAGwAAACIAAAAbAAAAIgAAAAYAAADZAAAAegAAAN4AAAB8AAAA4gAAAIEAAADnAAAAhQAAAOoAAACJAAAA6gAAAIkAAAA=";
BASE64Decoder based = new BASE64Decoder();
String strByteReture = "";
try {
byte[] b = based.decodeBuffer(strBase64);
for (int j = 0; j < b.length; j++) {
String strByte = Byte.toString(b[j]);
if(!strByte.contains("-")){
switch (strByte.length()) {
case 1:
strByte = "000"+strByte;break;
case 2:
strByte = "00"+strByte;break;
case 3:
strByte = "0"+strByte;break;
}
}else{
int lessZore = Integer.parseInt(strByte);
strByte = lessZore + 256+"";
switch (strByte.length()) {
case 1:
strByte = "000"+strByte;break;
case 2:
strByte = "00"+strByte;break;
case 3:
strByte = "0"+strByte;break;
}
}
strByteReture = strByteReture + strByte;
}
} catch (IOException e) {
e.printStackTrace();
}

return strByteReture.trim();
}

/**
* 根据byte位生成矢量图,前13*4 位是验证码,
* 17*4-13*4 位是宽度码
* 21*4-17*4 位是高度码
* 25*4-21*4 位是图片上线条数量 n
* 29*4-25*4 位是各跳线由多少各点组成(x1,y1,x2,y2)
* @param strImgPah
* @param strByte
*/
public void bulidBitImg(String strImgPah,String strByte){
strByte = strByte.substring(13*4);//去掉验证码
//宽
String strWidth = strByte.substring(0,4);//0240 0000 0000 0000
int width = Integer.parseInt(strWidth);//240
strByte = strByte.substring(4*4);
//高
String strHeigth = strByte.substring(0,4);//0147 0000 0000 0000
int height = Integer.parseInt(strHeigth);//147
strByte = strByte.substring(4*4);
// //线条数量
String strLineCount = strByte.substring(0,4);//0002 0000 0000 0000
int lineCount = Integer.parseInt(strLineCount);//2
strByte = strByte.substring(4*4);
image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.yellow);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
for (int n = 0; n < lineCount; n++) {
String strFirstPointCount = strByte.substring(0,4);//0010 0000 0000 0000
int firstPointCount = Integer.parseInt(strFirstPointCount);//10
strByte = strByte.substring(4*4);
//各条线上的点坐标位
String strPoints = strByte.substring(0 , 4*8*firstPointCount);//两个点决定一个坐标
strByte = strByte.substring(4*8*firstPointCount);
List<Integer> firstPointList = new ArrayList<Integer>();
for (int i = 0; i < strPoints.length(); i=i+4) {
if((i+4) < strPoints.length()){
String strFirstPoint = strPoints.substring(i, i+4);
int intFirstPoint = Integer.parseInt(strFirstPoint);
if(intFirstPoint > 0){
firstPointList.add(intFirstPoint);
}
}
}

for (int k = 0; k < firstPointList.size(); k=k+4) {
if(k+5 <= firstPointList.size()){
g.drawLine(firstPointList.get(k), firstPointList.get(k+1), firstPointList.get(k+2), firstPointList.get(k+3));
g.drawLine(firstPointList.get(k+2), firstPointList.get(k+3), firstPointList.get(k+4), firstPointList.get(k+5));
}
}
}
createJpg(strImgPah);
}

/**
* base64 to img
* @param strBase64
* @param strPath:ServletActionContext.getServletContext().getRealPath("/")/asd
* @return
*/
public static boolean getImgFromBase64(String strBase64,String strPath){
if(strBase64 == null)return false;
BASE64Decoder based = new BASE64Decoder();
try {
byte[] b = based.decodeBuffer(strBase64);
//strPath = ServletActionContext.getServletContext().getRealPath("/")+"pda\\"+strPath;
OutputStream out = new FileOutputStream(strPath);
out.write(b);
out.flush();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}


/**
* img to base64
* @param strPath
* @return
*/
public static String getBase64Img(String strPath){
byte[] b =null;
FileInputStream in =null;
if(strPath == null) return null;
try {
in = new FileInputStream(strPath);
b = new byte[in.available()];
in.read(b);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder basee = new BASE64Encoder();
return basee.encode(b);
}


/**
*
* String to BASE64
* @param str
* @return
*/
public static String getBASE64Str(String str){
String strBASE64Code = "" ;
if(str == null) return null;
BASE64Encoder base64 = new BASE64Encoder();
strBASE64Code = base64.encode(str.getBytes());
return strBASE64Code;
}

/**
* BASE64 to String
* @param strBASE64
* @return
*/
public static String getStrFromBASE64(String strBASE64){
String strString = "" ;
if(strBASE64 == null)return null;
BASE64Decoder based = new BASE64Decoder();
try {
byte[] b = based.decodeBuffer(strBASE64);
strString = new String(b);
return strString;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


public static void main(String[] args) {
Base64 b = new Base64();
String strBase64="CQAAAHNpZ25hdHVyZfAAAACTAAAAAgAAAAoAAAAFAAAABAAAAAkAAAAGAAAADwAAAAsAAAATAAAADwAAABcAAAASAAAAGAAAABYAAAAaAAAAGgAAABsAAAAeAAAAGwAAACIAAAAbAAAAIgAAAAYAAADZAAAAegAAAN4AAAB8AAAA4gAAAIEAAADnAAAAhQAAAOoAAACJAAAA6gAAAIkAAAA=";
String strByte=b.getByteFromBase64(strBase64);
String sPath="d:\\luojc.jpg";
b.bulidBitImg(sPath,strByte);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值