Java 和 JavaScript 之间真正通用的Base64编码

在开发Java  Web应用的时候,可能会在服务器端用Java做Base64编码,而在客户端用JavaScript进行解码。这样就要求两边的Base64编码机制保持一致。

使用Base64编码,可能会碰到各种奇怪情况,甚至怀疑编码有bug。但实际上不是这样的。Base64理论上操作的对象不是字符串而是字节数组。它的原理就是把ASCII码的255个字符缩小到用64个来表示。具体就是原来三个字节用四个字节表示,编码后长度有一定的增长。

1) 最好一次编码,避免分段编码,确实要分段编码,每一段字节数应该是3的倍数。

长字节流,如果要边读取边编码,每一段必须是3的倍数,否则就可能在还原的时候出乱。一般人喜欢用2的乘方来定义数组,例如 byte[1024],因为不是3的倍数,可能还原时出错。正确的例子是:

byte[] bs=new byte[3*100] ....inputStream.read(bs)......encode(bs )....

对于字符串,一般要整个一次编码,以避免分段编码出错。

当然,如果你分段编码,还原的时候也是一段一段地还原,那是没有问题的。

2)确保字符串还原的时候按照原来的编码还原。

因为它操作的是字节数组,所以对于GBK编码的汉字和UTF-8编码汉字,经过 Base64编码后结果是不一样的。例如“我们”这两个字如果是GBK编码,转成Base64后就是ztLDxw== ;如果是UTF-8编码,转成Base64后就是5oiR5Lus。

也就是 “我们” ==》  getBytes("GBK") ==> Base64

所以Java这边用什么编码转换,在JavaScript那边就要用什么编码还原。要保证Java和JavaScript通用,我们采用Unicode的编码(JavaScript转成UTF-8、GBK不方便,所以就采用了其本身的Unicode编码),具体如下:

服务器端:

1)用getBytes("Unicode")转成Unicode字节数组。

2) 编码成Base64字符串

3)传送到客户端

客户端:

1)Base64 解码成字节数组

2)按Unicode还原

代码如下(相关的函数看附件):

Base64.encode(data,"Unicode"); //java 端编码

decode64(data);   //javascript解码

 

 

附一:Java中Base64编码

package websharp.util;
public class Base64 {
    private static final byte[] encodingTable = {
            (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
            (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
            (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
            (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
            (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
            (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
            (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
            (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
            (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
            (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
            (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
            (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
            (byte) '8', (byte) '9', (byte) '+', (byte) '/'
        };
    private static final byte[] decodingTable;
    static {
        decodingTable = new byte[128];
        for (int i = 0; i < 128; i++) {
            decodingTable[i] = (byte) -1;
        }
        for (int i = 'A'; i <= 'Z'; i++) {
            decodingTable[i] = (byte) (i - 'A');
        }
        for (int i = 'a'; i <= 'z'; i++) {
            decodingTable[i] = (byte) (i - 'a' + 26);
        }
        for (int i = '0'; i <= '9'; i++) {
            decodingTable[i] = (byte) (i - '0' + 52);
        }
        decodingTable['+'] = 62;
        decodingTable['/'] = 63;
    }
    public static byte[] encode(byte[] data,int offset) {
        byte[] bytes;
        int realCount=data.length-offset;
        int modulus = realCount % 3;
        if (modulus == 0) {
            bytes = new byte[(4 * realCount) / 3];
        } else {
            bytes = new byte[4 * ((realCount / 3) + 1)];
        }
        int dataLength = (data.length - modulus);
        int a1;
        int a2;
        int a3;
        for (int i = offset, j = 0; i < dataLength; i += 3, j += 4) {
            a1 = data[i] & 0xff;
            a2 = data[i + 1] & 0xff;
            a3 = data[i + 2] & 0xff;
            bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
            bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
            bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
            bytes[j + 3] = encodingTable[a3 & 0x3f];
        }
        int b1;
        int b2;
        int b3;
        int d1;
        int d2;
        switch (modulus) {
        case 0: /* nothing left to do */
            break;
        case 1:
            d1 = data[data.length - 1] & 0xff;
            b1 = (d1 >>> 2) & 0x3f;
            b2 = (d1 << 4) & 0x3f;
            bytes[bytes.length - 4] = encodingTable[b1];
            bytes[bytes.length - 3] = encodingTable[b2];
            bytes[bytes.length - 2] = (byte) '=';
            bytes[bytes.length - 1] = (byte) '=';
            break;
        case 2:
            d1 = data[data.length - 2] & 0xff;
            d2 = data[data.length - 1] & 0xff;
            b1 = (d1 >>> 2) & 0x3f;
            b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
            b3 = (d2 << 2) & 0x3f;
            bytes[bytes.length - 4] = encodingTable[b1];
            bytes[bytes.length - 3] = encodingTable[b2];
            bytes[bytes.length - 2] = encodingTable[b3];
            bytes[bytes.length - 1] = (byte) '=';
            break;
        }
        return bytes;
    }
    public static byte[] decode(byte[] data) {
        byte[] bytes;
        byte b1;
        byte b2;
        byte b3;
        byte b4;
        data = discardNonBase64Bytes(data);
        if (data[data.length - 2] == '=') {
            bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
        } else if (data[data.length - 1] == '=') {
            bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
        } else {
            bytes = new byte[((data.length / 4) * 3)];
        }
        for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {
            b1 = decodingTable[data[i]];
            b2 = decodingTable[data[i + 1]];
            b3 = decodingTable[data[i + 2]];
            b4 = decodingTable[data[i + 3]];
            bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
            bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
            bytes[j + 2] = (byte) ((b3 << 6) | b4);
        }
        if (data[data.length - 2] == '=') {
            b1 = decodingTable[data[data.length - 4]];
            b2 = decodingTable[data[data.length - 3]];
            bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
        } else if (data[data.length - 1] == '=') {
            b1 = decodingTable[data[data.length - 4]];
            b2 = decodingTable[data[data.length - 3]];
            b3 = decodingTable[data[data.length - 2]];
            bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
            bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
        } else {
            b1 = decodingTable[data[data.length - 4]];
            b2 = decodingTable[data[data.length - 3]];
            b3 = decodingTable[data[data.length - 2]];
            b4 = decodingTable[data[data.length - 1]];
            bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
            bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
            bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
        }
        return bytes;
    }
    public static byte[] decode(String data) {
        byte[] bytes;
        byte b1;
        byte b2;
        byte b3;
        byte b4;
        data = discardNonBase64Chars(data);
        if (data.charAt(data.length() - 2) == '=') {
            bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
        } else if (data.charAt(data.length() - 1) == '=') {
            bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
        } else {
            bytes = new byte[((data.length() / 4) * 3)];
        }
        for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {
            b1 = decodingTable[data.charAt(i)];
            b2 = decodingTable[data.charAt(i + 1)];
            b3 = decodingTable[data.charAt(i + 2)];
            b4 = decodingTable[data.charAt(i + 3)];
            bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
            bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
            bytes[j + 2] = (byte) ((b3 << 6) | b4);
        }
        if (data.charAt(data.length() - 2) == '=') {
            b1 = decodingTable[data.charAt(data.length() - 4)];
            b2 = decodingTable[data.charAt(data.length() - 3)];
            bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
        } else if (data.charAt(data.length() - 1) == '=') {
            b1 = decodingTable[data.charAt(data.length() - 4)];
            b2 = decodingTable[data.charAt(data.length() - 3)];
            b3 = decodingTable[data.charAt(data.length() - 2)];
            bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
            bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
        } else {
            b1 = decodingTable[data.charAt(data.length() - 4)];
            b2 = decodingTable[data.charAt(data.length() - 3)];
            b3 = decodingTable[data.charAt(data.length() - 2)];
            b4 = decodingTable[data.charAt(data.length() - 1)];
            bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
            bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
            bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
        }
        for(int i=0;i<bytes.length;i++) System.out.println(","+bytes[i]);
        return bytes;
    }
    private static byte[] discardNonBase64Bytes(byte[] data) {
        byte[] temp = new byte[data.length];
        int bytesCopied = 0;
        for (int i = 0; i < data.length; i++) {
            if (isValidBase64Byte(data[i])) {
                temp[bytesCopied++] = data[i];
            }
        }
        byte[] newData = new byte[bytesCopied];
        System.arraycopy(temp, 0, newData, 0, bytesCopied);
        return newData;
    }
    private static String discardNonBase64Chars(String data) {
        StringBuffer sb = new StringBuffer();
        int length = data.length();
        for (int i = 0; i < length; i++) {
            if (isValidBase64Byte((byte) (data.charAt(i)))) {
                sb.append(data.charAt(i));
            }
        }
        return sb.toString();
    }
    private static boolean isValidBase64Byte(byte b) {
        if (b == '=') {
            return true;
        } else if ((b < 0) || (b >= 128)) {
            return false;
        } else if (decodingTable[b] == -1) {
            return false;
        }
        return true;
    }
    public static String  encode(String data,String charset)throws Exception
    {
        // byte[] result =  (data.getBytes("Unicode"));
         if(data==null || data.length()==0) return data;
         int offset=0;
         // getBytes("unicode")转完后会在前头加上两字节”FE“
         byte[] result=encode (data.getBytes(charset),offset);
         StringBuffer sb=new StringBuffer(result.length);
         for (int i=0;i<result.length;i++)   sb.append((char)result[i]);
         return sb.toString();
    }
    public static String  decode(String data,String charset)throws Exception
    {
        if(data==null || data.length()==0) return data;
        return new String(Base64.decode(data),charset);
    }
    public static void main(String[] args) throws Exception {
        String data = "我们";
        String data1=encode(data,"Unicode");
        String data2=decode(data1,"Unicode");
        System.out.println(data);
        System.out.println(data1);
        System.out.println(data2);
    }
}


附二:JavaScript中Base64编码

<html>
<head>
<title>base64 Encoding/Decoding</title>
</head>
<script type="text/javascript"><!--
var keyStr = "ABCDEFGHIJKLMNOP" +
             "QRSTUVWXYZabcdef" +
             "ghijklmnopqrstuv" +
             "wxyz0123456789+/" +
             "=";

function encode64(input) {
   input = unicodetoBytes(input);
   var output = "";
   var chr1, chr2, chr3 = "";
   var enc1, enc2, enc3, enc4 = "";
   var i = 0;

   do {
      chr1 = input[i++];
      chr2 = input[i++];
      chr3 = input[i++];

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output +
         keyStr.charAt(enc1) +
         keyStr.charAt(enc2) +
         keyStr.charAt(enc3) +
         keyStr.charAt(enc4);
      chr1 = chr2 = chr3 = "";
      enc1 = enc2 = enc3 = enc4 = "";
   } while (i < input.length);

   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3 = "";
   var enc1, enc2, enc3, enc4 = "";
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   var base64test = /[^A-Za-z0-9/+///=]/g;
   if (base64test.exec(input)) {
      alert("There were invalid base64 characters in the input text./n" +
            "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='/n" +
            "Expect errors in decoding.");
   }
   input = input.replace(/[^A-Za-z0-9/+///=]/g, "");
    output=new Array();
   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output.push(chr1);
      if (enc3 != 64) {
         output.push(chr2);
      }
      if (enc4 != 64) {
         output.push(chr3);
      }

      chr1 = chr2 = chr3 = "";
      enc1 = enc2 = enc3 = enc4 = "";

   } while (i < input.length);
   return bytesToUnicode(output);
}

  function unicodetoBytes(s)
   {
      var result=new Array();
      if(s==null || s=="") return result;
      result.push(255); // add "FE" to head
      result.push(254);
      for(var i=0;i<s.length;i++)
      {
        var c=s.charCodeAt(i).toString(16);
        if(c.length==1) i="000"+c;
        else if(c.length==2) c="00"+c;
        else if(c.length==3) c="0"+c;
        var var1=parseInt( c.substring(2),16);
        var var2=parseInt( c.substring(0,2),16);
        result.push( var1);
        result.push(var2) ;
      }
      return result;
   }

   function bytesToUnicode(bs)
   {
     var result="";
      var offset=0;
      if(bs.length>=2 && bs[0]==255 && bs[1]==254) offset=2;  // delete "FE"
      for(var i=offset;i<bs.length;i+=2)
      {
            var code=bs[i]+(bs[i+1]<<8);
            result+=String.fromCharCode(code);
      }
      return result;
   }
//-->
</script>
<body>
<form name="base64Form">
   Type in the message you want to encode in base64, or paste<br>
   base64 encoded text into the text field, select Encode or Decode, <br>
   and click the button!<br>

   <textarea name="theText" cols="40" rows="6"></textarea><br>

   <input type="button" name="encode" value="Encode to base64"
      onClick="document.base64Form.theText.value=encode64(document.base64Form.theText.value);">
   <input type="button" name="decode" value="Decode from base64"
      onClick="document.base64Form.theText.value=decode64(document.base64Form.theText.value);">
</form>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 要在Javascript中直接调用安卓相机并返回base64编码,需要使用Cordova或其他类似的移动应用开发框架。这些框架提供了访问设备硬件的API,以便从Javascript中调用原生代码。 以下是一些步骤来实现这个目标: 1. 安装Cordova并创建一个新项目。 2. 添加一个插件来访问相机API,例如cordova-plugin-camera。 3. 在Javascript代码中编写一个函数来打开相机并获取照片。这可以通过调用navigator.camera.getPicture()实现。您可以指定选项来配置相机行为,例如图像质量、旋转等等。 4. 在获取照片后,您可以使用window.atob()函数将图像数据从base64字符串解码为二进制数据。 5. 最后,您可以将二进制数据编码base64字符串,以便在Javascript代码中进行处理。 以下是一个示例代码片段,演示了如何使用Cordova和cordova-plugin-camera插件来获取照片并将其编码base64字符串: ```javascript // 点击按钮时调用此函数 function takePicture() { navigator.camera.getPicture( function(imageData) { // 将base64编码的图像数据解码为二进制数据 var binaryData = window.atob(imageData); // 创建一个Uint8Array以便访问二进制数据 var bytes = new Uint8Array(binaryData.length); for (var i = 0; i < binaryData.length; i++) { bytes[i] = binaryData.charCodeAt(i); } // 将二进制数据编码base64字符串 var base64Data = btoa(String.fromCharCode.apply(null, bytes)); // 现在您可以在这里处理base64Data字符串 }, function(errorMessage) { console.log('获取照片失败: ' + errorMessage); }, { quality: 50, // 图像质量 destinationType: Camera.DestinationType.DATA_URL // 返回base64编码的图像数据 } ); } ``` 请注意,此代码假设您已经正确安装了Cordova和cordova-plugin-camera插件,并且已经在设备上进行了正确配置。 ### 回答2: 使用JavaScript调用Android相机并返回Base64编码的步骤如下: 1. 首先,你需要创建一个HTML页面,并在其中引入JavaScript代码。你可以使用以下代码作为起始点: ```html <!DOCTYPE html> <html> <head> <title>调用Android相机并返回Base64编码</title> </head> <body> <button id="capture-btn">拍照</button> <script> // JavaScript代码将在这里插入 </script> </body> </html> ``` 2. 在JavaScript代码部分,你需要使用JavaScript与Android原生代码进行通信。可以使用JavaScript的`Window`对象的`WebView`接口与安卓进行通信。 ```javascript <script> var captureBtn = document.getElementById('capture-btn'); // 在按钮被点击时调用Android相机 captureBtn.addEventListener('click', function() { window.android.capturePhoto(); // 调用Android的拍照方法 }); // 接收从Android返回的Base64编码 window.receiveBase64 = function(base64) { // 在这里使用Base64编码 console.log(base64); }; </script> ``` 3. 在你的Android原生代码中,你需要通过WebView设置接口以便JavaScript代码可以调用它。以下是一个示例: ```java import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Base64; import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); // 添加JavaScript接口 webView.addJavascriptInterface(this, "android"); // 设置WebViewClient以使WebView在应用内加载链接 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }); // 加载你的HTML页面 webView.loadUrl("file:///android_asset/index.html"); } // 用于从Android调用JavaScript方法的方法 @JavascriptInterface public void capturePhoto() { // 打开相机 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); } // 在拍照完成后返回结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && data != null) { Uri uri = data.getData(); // 将图像转换为Base64编码 String base64 = imageToBase64(uri); // 将Base64编码发送给JavaScript webView.loadUrl("javascript:receiveBase64('" + base64 + "')"); } } // 将图像转换为Base64编码的辅助方法 private String imageToBase64(Uri uri) { // 从URI中读取图像数据 // 将图像数据转换为Base64编码字符串 // 返回Base64编码字符串 } } ``` 这就是使用JavaScript调用Android相机并返回Base64编码的基本步骤。你需要根据你的实际需求进行适当的调整和完善。请注意,上述代码仅供参考,并且可能需要根据你的项目进行额外的自定义和错误处理。 ### 回答3: 要使用JavaScript直接调用Android相机并返回基于base64编码,可以使用HTML5中的getUserMedia API和Canvas元素来实现。以下是一个简单的例子: 首先,在HTML中添加一个用于显示摄像头捕获图像的Canvas元素: ``` <canvas id="cameraOutput"></canvas> ``` 接下来,在JavaScript中获取用户媒体流并将其显示在Canvas上: ``` const canvas = document.getElementById('cameraOutput'); const context = canvas.getContext('2d'); navigator.mediaDevices.getUserMedia({ video: true }) .then(function(stream) { const video = document.createElement('video'); video.srcObject = stream; video.play(); // 每帧都将视频图像绘制到Canvas中 const drawFrame = function() { context.drawImage(video, 0, 0, canvas.width, canvas.height); requestAnimationFrame(drawFrame); }; drawFrame(); }) .catch(function(error) { console.error('无法访问相机:', error); }); ``` 现在,我们有了相机捕获的图像显示在Canvas上。然后,我们可以通过将Canvas上的图像转换为base64编码来获取图像数据: ``` const canvas = document.getElementById('cameraOutput'); const context = canvas.getContext('2d'); // 获取Canvas上的图像数据 const getImageData = function() { return canvas.toDataURL('image/jpeg', 1.0); }; // 调用此函数以获取基于base64的图像编码 const base64Image = getImageData(); console.log(base64Image); ``` 通过以上方法,我们可以直接在JavaScript中调用Android相机并返回基于base64的图像编码。请注意,这仅适用于支持getUserMedia API的现代浏览器和设备。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值