http://bbs.csdn.net/topics/390365358
这个是之前的求助帖子,帖子中说用控件解决,由于我是学JAVA的,比葫芦画瓢终于算是把这个控件弄出了个大概,目前控件提供两个方法,一个是删除文件,一个是读取图片并返回BASE64编码。
初步打算是读完身份证以后,图片位置是已知的,那么我可以读取这个图片成BASE64,先后将这个BASE64发给后台,后台经过处理再变成图片储存,然后调用控件的删除文件方法,将图片文件删除。
现在删除文件方法经测试没有问题,但是将图片解析成BASE64的这个方法总是有乱码。
现给出源代码,希望各位不吝赐教啊,C++我完全不会,完全是按照JAVA的思想比葫芦画瓢,到底是哪里出了问题,实在是无能为力,而且每一次改动程序都要重新生成控件,再制作测试的HTML页面,相当麻烦。所以干脆直接求助吧,如果这是一段JAVA程序,我保证看到结果就大概知道问题出在哪里,因为这只是一个简单的逻辑,读取文件为二进制,然后将二进制转化为BASE64。
我给出重要代码。
Base64.h
#include <string>std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s);Base64.cpp
/* 
   base64.cpp and base64.h   Copyright (C) 2004-2008 René Nyffenegger   This source code is provided 'as-is', without any express or implied
   warranty. In no event will the author be held liable for any damages
   arising from the use of this software.   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:   1. The origin of this source code must not be misrepresented; you must not
      claim that you wrote the original source code. If you use this source code
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.   2. Altered source versions must be plainly ed as such, and must not be
      misrepresented as being the original source code.   3. This notice may not be removed or altered from any source distribution.   René Nyffenegger [email protected]*/
#include "stdafx.h"
#include "base64.h"
#include <iostream>
#include <comdef.h>
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline bool is_base64(unsigned char c) {
  return (isalnum(c) || (c == '+') || (c == '/'));
}std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  std::string ret;
  int i = 0;
  int j = 0;
  unsigned char char_array_3[3];
  unsigned char char_array_4[4];  while (in_len--) {
    char_array_3[i++] = *(bytes_to_encode++);
    if (i == 3) {
      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
      char_array_4[3] = char_array_3[2] & 0x3f;      for(i = 0; (i <4) ; i++)
        ret += base64_chars[char_array_4[i]];
      i = 0;
    }
  }  if (i)
  {
    for(j = i; j < 3; j++)
      char_array_3[j] = '\0';    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
    char_array_4[3] = char_array_3[2] & 0x3f;    for (j = 0; (j < i + 1); j++)
      ret += base64_chars[char_array_4[j]];    while((i++ < 3))
      ret += '=';  }  return ret;}std::string base64_decode(std::string const& encoded_string) {
  int in_len = encoded_string.size();
  int i = 0;
  int j = 0;
  int in_ = 0;
  unsigned char char_array_4[4], char_array_3[3];
  std::string ret;  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
    char_array_4[i++] = encoded_string[in_]; in_++;
    if (i ==4) {
      for (i = 0; i <4; i++)
        char_array_4[i] = base64_chars.find(char_array_4[i]);      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];      for (i = 0; (i < 3); i++)
        ret += char_array_3[i];
      i = 0;
    }
  }  if (i) {
    for (j = i; j <4; j++)
      char_array_4[j] = 0;    for (j = 0; j <4; j++)
      char_array_4[j] = base64_chars.find(char_array_4[j]);    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  }  return ret;
}
以上是从英文网站COPY的,下面是控件内的获取方法。
BSTR CFAXCtrl::getFileBase64(LPCTSTR filePath)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); BSTR bstrResult; // TODO: 在此添加调度处理程序代码
try{
CFile file(filePath, CFile::modeRead);
char *fileBuf = NULL; DWORD length = (DWORD)file.GetLength();
fileBuf = new char[length + 1];
        memset(fileBuf, 0, sizeof(char) * (length + 1));
UINT readLength = file.Read(fileBuf, length);
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(fileBuf),readLength);
_bstr_t bstr_t(encoded.c_str());//取encoded的字符串形式
        bstrResult = bstr_t.GetBSTR(); //得到BSTR 
        delete[] fileBuf;
        fileBuf = NULL;
}catch(CException* e){
e->ReportError();
    e->Delete();
}
return bstrResult;
}以上内容全是我COPY组织的,对于其深切含义完全不明白,所以看到解析的BASE64不对,有点无从下手,希望高手指点。mfcCfile控件base64

解决方案 »

  1.   

    base64 转成图片和控件有什么关系!
      

  2.   

    直接找http://club.topsage.com/thread-531081-1-1.html
      

  3.   


    你没看第一篇吧,老兄啊,有个问题需要用控件解决,解决的时候需要将文件转成BASE64的一个方法,还有你说反了啊,是图片转成BASE64,不是BASE64转成图片,不考虑上一篇帖子的需求,简单点说就是,我在做一个MFC控件,控件的作用就是将一个图片读取成BASE64。然后有了如上程序,程序会出乱码。就这问题。
      

  4.   

    我发现了一个问题,谁能解答下,我发现到encoded的值好像是正确的,但是转成BSTR的时候,前面多了这些,大家来看下,0x0035e834,这是什么玩意?