`
noknower
  • 浏览: 120161 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
阅读更多

根据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);
   }
分享到:
评论

相关推荐

    pda传送图片代码

    根据给定文件的信息,我们可以提炼出关于PDA传送图片到Web服务器的相关知识点,涉及C#编程、PDA设备(如Pocket PC)、Web开发(包括ASP.NET)以及网络通信技术。 ### PDA传送图片代码的核心概念 #### PDA设备与...

    wince 5.0 6.0 PDA同步工具WIN7-64安装包

    标题中的“wince 5.0 6.0 PDA同步工具WIN7-64安装包”指的是一个专为Windows CE 5.0和6.0操作系统设计的个人数字助手(PDA)与运行Windows 7 64位操作系统的计算机进行同步的软件安装程序。这个安装包旨在解决在64位...

    服务于PDA的矢量数据分块存储数据结构的研究.pdf

    该数据结构的基本思想是将大的矢量数据集分割成多个小块,每个小块包含地图的一部分,并且可以根据用户的地理位置和视图需求动态加载到内存中。这种分块策略不仅可以显著减少内存占用,还能提高数据读取和显示的速度...

    PDA连接工具win8同步中心64位

    标题中的“PDA连接工具win8同步中心64位”指的是在Windows 8 64位操作系统环境下,用于连接个人数字助手(Personal Digital Assistant,PDA)的同步管理软件。这个工具是专为64位架构设计的,以确保与Windows 8系统...

    MC68VZ328微处理器掌上电脑PDA电路原理图

    其电路原理图是理解PDA内部架构和工作原理的关键,也是进行故障诊断与维修的重要依据。本文将深入解析MC68VZ328微处理器在PDA中的作用及其电路设计要点。 ### MC68VZ328微处理器简介 MC68VZ328是由摩托罗拉公司...

    TQ6410PDA原理图

    广州天嵌科技TQ6410PDA PCB格式原理图。图上包括:音频,SD卡,U盘,wifi接口,camera接口,多串口布局,网络等功能。

    pda_2443原理图sch

    ### PDA_2443原理图Sch:三星6410替换产品的深入解析 #### 概述 在IT行业中,对于硬件工程师和技术爱好者来说,理解特定设备的电路原理图至关重要,它不仅揭示了设备内部的工作机制,还为设计、故障排查和改进提供...

    《深海迷航》PDA地图MOD

    4. **自定义设置**:MOD还可能提供了自定义选项,玩家可以根据自己的需求调整地图的显示样式和信息密度,打造个性化的游戏体验。 5. **兼容性**:一个好的MOD需要与游戏的其他组件兼容,不会引发冲突或导致游戏崩溃...

    PDA端使用PDA抓屏软件

    在IT行业中,PDA(Personal Digital Assistant)是一种便携式电子设备,通常用于个人日程管理、联系人存储、任务管理等。随着技术的发展,PDA也开始具备了更丰富的功能,比如屏幕抓取,这对于软件演示、故障排查、...

    PDA连接工具win8同步中心64位.exe

    标题中的"PDA连接工具win8同步中心64位.exe"是指一种专为Windows 8操作系统设计的64位版本的个人数字助手(PDA)同步软件。在IT领域,PDA通常指的是掌上电脑,是一种便携式电子设备,用于管理个人信息,如联系人、...

    C#开发PDA程序

    5. 应用打包与部署:了解如何将源码编译成可执行文件,并在实际PDA设备上安装和运行。 通过深入学习这个C#开发的PDA程序源码,初学者不仅可以掌握C#编程基础,还能了解到面向嵌入式系统的软件开发流程和技巧,为...

    PDA开发环境配置

    【PDA开发环境配置】是针对使用PDA设备进行软件开发的过程,主要涉及的工具包括Visual Studio 2008、Windows Mobile 6 Professional SDK及相关插件。以下将详细阐述配置过程及其相关知识点: 首先,硬件准备是PDA...

    TTS+PDA.rar(PDA设备扫描条码并通过TTS自动播报)

    在IT行业中,PDA(Personal Digital Assistant,个人数字助手)是一种便携式设备,常用于数据采集和管理,尤其在物流、仓储、零售等领域。在这个名为"TTS+PDA.rar"的压缩包文件中,包含了一个针对PDA设备的演示程序...

    CFG-Convert-to-PDA.rar_PDA grammar_cfg_pda cfg_文法 pda

    标题中的“CFG-Convert-to-PDA.rar”表明这是一个关于将上下文无关文法(CFG)转换为推导堆栈自动机(PDA)的压缩文件。这个过程在理论计算机科学中很重要,因为CFGs和PDAs是形式语言理论的基础概念,它们在编译器...

    WinCE PDA开发调用WebService源码 内附代码及演示过程图片

    在本文中,我们将深入探讨如何在WinCE平台上使用PDA进行开发,并调用WebService服务。这一主题涵盖了嵌入式系统开发、移动应用编程以及网络通信技术。我们将详细讲解使用Visual Studio 2005作为开发工具,以及.NET ...

    pda巡查系统+pda地图的b/s结构j2ee的GIS系统实现

    【标题】:“pda巡查系统+pda地图的b/s结构j2ee的GIS系统实现”这一主题涉及到的是在IT行业中,如何利用PDA(个人数字助手)设备,结合GIS(地理信息系统)技术,通过B/S(浏览器/服务器)架构来构建一个高效的巡查...

    PDA查看注册表工具

    标题中的“PDA查看注册表工具”指的是一个专门设计用于个人数字助手(PDA)设备的应用程序,该程序允许用户查看并可能编辑其设备上的注册表。注册表是Windows操作系统中的一个重要组成部分,它存储着系统和应用程序...

    获取PDA的IP和MAC

    在IT领域,PDA(Personal Digital Assistant)是个人数字助手的简称,通常指的是掌上电脑或智能手机等便携式设备。这些设备可以通过各种方式与个人计算机进行通信,以便同步数据、传输文件或进行调试。在标题"获取...

    PDA 程序 扫描头

    在IT行业中,PDA(Personal Digital Assistant)是一种便携式电子设备,通常用于个人信息管理、数据采集和通信。"PDA程序 扫描头"这个标题暗示了我们正在讨论一个针对PDA设备开发的特定应用,该应用的核心功能是利用...

    PdaNet IOS WIN7桌面版 64位

    PdaNet IOS WIN7桌面版 64位,好东西自己在用的啊

Global site tag (gtag.js) - Google Analytics