本文的目的是介绍在Swift中从JSON解码base64_encode图像的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于base64_encode和base64_dec
本文的目的是介绍在Swift中从JSON解码base64_encode图像的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于base64_encode 和 base64_decode 的 JAVA 实现、base64_encode 的问题、base64_encode与base64_decode、base64_encode加密中文字符使用java乱码怎么办的知识。
本文目录一览:- 在Swift中从JSON解码base64_encode图像
- base64_encode 和 base64_decode 的 JAVA 实现
- base64_encode 的问题
- base64_encode与base64_decode
- base64_encode加密中文字符使用java乱码怎么办
在Swift中从JSON解码base64_encode图像
我有一个包含一些图像的mysql数据库。我从php文件接收数据:
php: $result[$key][''image''] = based64_encode($resultArray[$key][''image'']);
现在有了一个Json文件,我得到的是这样的:
Json:{"image":"\/9j\/4Q\/+RXhpZgAATU0AKgAAAAgACgEPAAIAAAAGAAAAhgEQAAIAAAAKAAAAjAESAAMAAAABAAYAAAEaAAUAAAABAAAAlgEbAAUAAAABAAAAngEoAAMAAAABAAIAAE...
我有一个迅速的项目,想将图像解码成UIImage,到目前为止,我还不知道如何解码图像。我有以下几点。
Swift:Alamofire.request(.GET, url).responseJSON { (response) -> Void in if let JSON = response.result.value as? [[String : AnyObject]]{ for json in JSON{ JSON let encodedImage = json["image"] let imageData = NSData(base64EncodedString: encodedImage) } }
如何解码图像以便显示?
答案1
小编典典您必须将字典值从AnyObject强制转换为String。您还必须使用.IgnoreUnknownCharacters选项对字符串数据进行解码。这样尝试
if let encodedImage = json["image"] as? String, imageData = NSData(base64EncodedString: encodedImage, options: .IgnoreUnknownCharacters), image = UIImage(data: imageData) { print(image.size)}
雨燕3.0.1•Xcode 8.1
if if let encodedImage = json["image"] as? String, let imageData = Data(base64Encoded: encodedImage, options: .ignoreUnknownCharacters), let image = UIImage(data: imageData) { print(image.size)}
base64_encode 和 base64_decode 的 JAVA 实现
Base64 是网络上最常见的用于传输 8Bit 字节代码的编码方式之一,大家可以查看 RFC2045~RFC2049,上面有 MIME 的详细规范。 Base64 要求把每三个 8Bit 的字节转换为四个 6Bit 的字节(3*8 = 4*6 = 24),然后把 6Bit 再添两位高位 0,组成四个 8Bit 的字节,也就是说,转换后的字符串理论上将要比原来的长 1/3php 的函数:base64_encode () 和 base64_decode ()
base64 的编,解码原理
Base64 编码其实是将 3 个 8 位字节转换为 4 个 6 位字节,(3*8 = 4*6 = 24) 这 4 个六位字节 其实仍然是 8 位,只不过高两位被设置为 0. 当一个字节只有 6 位有效时,它的取值空间为 0 到 2 的 6 次方减 1 即 63, 也就是说被转换的 Base64 编码的每一个编码的取值空间为 (0~63) 。
事实上,0~63 之间的 ASCII 码有许多不可见字符,所以应该再做一个映射,映射表为
''A'' ~ ''Z'' ? ASCII(0 ~ 25)
''a'' ~ ''z'' ? ASCII(26 ~ 51)
''0'' ~ ''9'' ? ASCII(52 ~ 61)
'' '' ? ASCII(62)
''/'' ? ASCII(63)
这样就可以将 3 个 8 位字节,转换为 4 个可见字符。
具体的字节拆分方法为:(图(画得不好,领会精神 :-))
aaaaaabb ccccdddd eeffffff //abcdef 其实就是 1 或 0,为了看的清楚就用 abcdef 代替
~~~~~~~~ ~~~~~~~~ ~~~~~~~~
字节 1 字节 2 字节 3
||
\/
00aaaaaa 00bbcccc 00ddddee 00ffffff
注:上面的三个字节位原文,下面四个字节为 Base64 编码,其前两位均为 0。
这样拆分的时候,原文的字节数量应该是 3 的倍数,当这个条件不能满足时,用全零字节
补足,转化时 Base64 编码用 = 号代替,这就是为什么有些 Base64 编码以一个或两个等号结
束的原因,但等号最多有两个,因为:如果 F (origin) 代表原文的字节数,F (remain) 代
表余数,则
F (remain) = F (origin) MOD 3 成立。
所以 F (remain) 的可能取值为 0,1,2.
如果设 n = [F (origin) – F (remain)] / 3
当 F (remain) = 0 时,恰好转换为 4*n 个字节的 Base64 编码。
当 F (remain) = 1 时,由于一个原文字节可以拆分为属于两个 Base64 编码的字节,为了
让 Base64 编码是 4 的倍数,所以应该为补 2 个等号。
当 F (remain) = 2 时,由于两个原文字节可以拆分为属于 3 个 Base64 编码的字节,同理,
应该补上一个等号。
base64 编码后的字符串末尾会有 0 到 2 个等号,这些等号在解码是并不必要,所以可以删除。
在网络 GET 和 POST 参数列表的时候,‘+’不能正常传输,可以把它替换成‘|’
这样经过 base64 编码后的字符串就只有‘|’和‘/‘,所以经过这样处理 base64 编码的字符串可以作为参数列表的以个参数值来传输
========================================================================
以下是老外写的一个实现:
package com.meterware.httpunit;
/********************************************************************************************************************
* $Id: Base64.java,v 1.4 2002/12/24 15:17:17 russgold Exp $
*
* Copyright (c) 2000-2002 by Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software "), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS ", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
/**
* A utility class to convert to and from base 64 encoding.
*
* @author <a href= "mailto:russgold@httpunit.org "> Russell Gold </a>
**/
public class Base64 {
final static String encodingChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ ";
/**
* Returns the base 64 encoded equivalent of a supplied string.
* @param source the string to encode
*/
public static String encode( String source ) {
char[] sourceBytes = getPaddedBytes( source );
int numGroups = (sourceBytes.length + 2) / 3;
char[] targetBytes = new char[4];
char[] target = new char[ 4 * numGroups ];
for (int group = 0; group < numGroups; group++) {
convert3To4( sourceBytes, group*3, targetBytes );
for (int i = 0; i < targetBytes.length; i++) {
target[ i + 4*group ] = encodingChar.charAt( targetBytes[i] );
}
}
int numPadBytes = sourceBytes.length - source.length();
for (int i = target.length-numPadBytes; i < target.length; i++) target[i] = ''= '';
return new String( target );
}
private static char[] getPaddedBytes( String source ) {
char[] converted = source.toCharArray();
int requiredLength = 3 * ((converted.length+2) /3);
char[] result = new char[ requiredLength ];
System.arraycopy( converted, 0, result, 0, converted.length );
return result;
}
private static void convert3To4( char[] source, int sourceIndex, char[] target ) {
target[0] = (char) ( source[ sourceIndex ] > > > 2);
target[1] = (char) (((source[ sourceIndex ] & 0x03) < < 4) | (source[ sourceIndex+1 ] > > > 4));
target[2] = (char) (((source[ sourceIndex+1 ] & 0x0f) < < 2) | (source[ sourceIndex+2 ] > > > 6));
target[3] = (char) ( source[ sourceIndex+2 ] & 0x3f);
}
/**
* Returns the plaintext equivalent of a base 64-encoded string.
* @param source a base 64 string (which must have a multiple of 4 characters)
*/
public static String decode( String source ) {
if (source.length()%4 != 0) throw new RuntimeException( "valid Base64 codes have a multiple of 4 characters " );
int numGroups = source.length() / 4;
int numExtraBytes = source.endsWith( "== " ) ? 2 : (source.endsWith( "= " ) ? 1 : 0);
byte[] targetBytes = new byte[ 3*numGroups ];
byte[] sourceBytes = new byte[4];
for (int group = 0; group < numGroups; group++) {
for (int i = 0; i < sourceBytes.length; i++) {
sourceBytes[i] = (byte) Math.max( 0, encodingChar.indexOf( source.charAt( 4*group+i ) ) );
}
convert4To3( sourceBytes, targetBytes, group*3 );
}
return new String( targetBytes, 0, targetBytes.length - numExtraBytes );
}
private static void convert4To3( byte[] source, byte[] target, int targetIndex ) {
target[ targetIndex ] = (byte) (( source[0] < < 2) | (source[1] > > > 4));
target[ targetIndex+1 ] = (byte) (((source[1] & 0x0f) < < 4) | (source[2] > > > 2));
target[ targetIndex+2 ] = (byte) (((source[2] & 0x03) < < 6) | (source[3]));
}
}
---------------------------
base64_encode 的问题
国家天气接口上面有一个 key 的算法说明
key=base64_encode(hash_hmac(''sha1'',$public_key,$private_key,TRUE));
用 swift 如何解释呢。。。。实在想不通。。。
base64_encode与base64_decode
-
base64_encode
编辑
函数说明
编辑函数定义
编辑实例说明
例-1
-
base64_decode
编辑
- 中文名
- base64_decode
- 用 途
- 使用 MIME base64 编码数据解码
- 相 关
- 返回的数据可能是 二进制的
- 结 果
- 返回 原始数据,失败则返回 FALSE
目录
- 1 函数定义
- 2 实例说明
- ▪ 例-1
- ▪ 例-2
函数定义
编辑实例说明
编辑例-1
base64_encode加密中文字符使用java乱码怎么办
base64_encode加密中文字符使用java乱码的解决办法:
问题:
Example: “蘋果君华”几个字。
一开始使用的是php urlencode,然后java urldecode,“蘋”字亂碼。
后改用PHP base64_encode,再使用java decode(使用的org.apache.commons.codec.binary.Base64包),"蘋"字OK了,但“君华”倆字亂碼了。
立即学习“Java免费学习笔记(深入)”;
search說是java使用的是非標準的base64解碼,so...求破。
解决方法:
会乱码的原因是你的编码不一致导致的
php中的urlencode的编码是和系统编码一致的(比如windows默认gb2312,ubuntu默认utf-8)
所以首先需要确定你的系统编码,之后根据得到的系统编码在调用java的decode方法的时候,将这个编码传入(考虑到你的例子中有繁体字,所以,建议你使用utf-8编码),以下是我使用utf-8编码的例子(php环境是ubuntun下)
<?php echo urlencode("蘋果君华"); ?> output: %E8%98%8B%E6%9E%9C%E5%90%9B%E5%8D%8E String ret = java.net.URLDecoder.decode("%E8%98%8B%E6%9E%9C%E5%90%9B%E5%8D%8E", "utf-8"); System.out.println(ret); output: 蘋果君华
更多相关技术文章,请访问PHP中文网!
我们今天的关于在Swift中从JSON解码base64_encode图像的分享就到这里,谢谢您的阅读,如果想了解更多关于base64_encode 和 base64_decode 的 JAVA 实现、base64_encode 的问题、base64_encode与base64_decode、base64_encode加密中文字符使用java乱码怎么办的相关信息,可以在本站进行搜索。
本文标签: