不能够转换位图,以完善的Base64字符串中的Android? [英] Not able to convert Bitmap to perfect Base64 String in Android?
问题描述
我的工作中,我需要从摄像头中捕获图像的应用程序。拍摄结束后,我要的转换为位图转换为Base64 。转换为Base64后,我一定要这个字符串发送给服务器。我使用低于code此任务:
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
image.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
byte []的B = baos.toByteArray();
base64Image = Base64.en codeToString(B,Base64.DEFAULT);
问题::当我将其转换Base64的图像,我得到图像不完整。同样的结果是发生在那里我的形象是不完全的Base64的字符串重新构建服务器。
请给我建议的解决方案。我已经查了,很多东西,得到相同的code的,我使用的是现在。
编辑:请参阅下面的不完整的图片
code使用捕捉图像:
意图=新的意图(android.media.action.IMAGE_CAPTURE);
startActivityForResult(意向,TAKE_PHOTO);
创建自己的的Base64
类,并使用这样的:
公共类的Base64
{
私人的Base64()
{
超();
}
/ **
*恩code一些数据,并返回一个字符串。
* /
公共最后静态字符串连接code(byte []的D)
{
如果(D == NULL)返回NULL;
字节的数据[] =新的字节[d.length + 2];
System.arraycopy(D,0,数据,0,d.length);
字节DEST [] =新的字节[(data.length / 3)* 4]。
// 3字节到4个字节的转换
对于(INT SIDX = 0,didx = 0; SIDX< d.length; SIDX + = 3,didx + = 4)
{
DEST [didx] =(字节)((数据[SIDX]≥>→2)及077);
目标寄存器[didx + 1] =(字节)((数据[SIDX + 1] GT;>&→4)及017 |
(数据[SIDX]其中; 4;)及077);
目标寄存器[didx + 2] =(字节)((数据[SIDX + 2]≥>→6)及003 |
(数据[SIDX + 1]; 2)及077);
目标寄存器[didx + 3] =(字节)(数据[SIDX + 2]安培; 077);
}
// 0-63为ASCII可转换
对于(INT IDX = 0; IDX< dest.length; IDX ++)
{
如果(DEST [IDX]< 26)DEST [IDX] =(字节)(DEST [IDX] +'A');
否则,如果(DEST [IDX]< 52)DEST [IDX] =(字节)(DEST [IDX] +'一' - 26);
否则,如果(DEST [IDX]< 62)DEST [IDX] =(字节)(DEST [IDX] +'0' - 52);
否则,如果(DEST [IDX]< 63)DEST [IDX] =(字节)'+';
别的目标寄存器[IDX] =(字节)/;
}
//添加填充
为(中间体IDX = dest.length-1; IDX>(d.length * 4)/ 3; idx--)
{
DEST [IDX] =(字节)=;
}
返回新的String(DEST);
}
/ **
使用Base64 *恩codeA字符串使用默认的平台编码
** /
公共最后静态字符串连接code(String s)将{
返回EN code(s.getBytes());
}
/ **
*德code数据并返回字节。
* /
公共最后静态的byte []德code(字符串str)
{
如果(STR == NULL)返回NULL;
字节数据[] = str.getBytes();
返回去code(数据);
}
/ **
*德code数据并返回字节。假定数据传递
*在为ASCII文本。
* /
公共最后静态的byte []德code(byte []的数据)
{
INT尾= data.length;
而(数据[尾1] =='=')tail--;
字节DEST [] =新的字节[尾 - data.length / 4]。
// ASCII可到0-63转换
对于(INT IDX = 0; IDX< data.length; IDX ++)
{
如果(数据[IDX] =='=')数据[IDX] = 0;
否则,如果(数据[IDX] =='/')数据[IDX] = 63;
否则,如果(数据[IDX] =='+')数据[IDX] = 62;
否则如果(数据[IDX]≥='0'和;&安培;数据[IDX]其中; ='9')
数据[IDX] =(字节)(数据[IDX] - ('0' - 52));
否则如果(数据[IDX]≥='一'和;&安培;数据[IDX]其中; ='Z')
数据[IDX] =(字节)(数据[IDX] - ('A' - 26));
否则,如果(数据[IDX]≥='A'和;&放大器;数据[IDX]< ='Z')
数据[IDX] =(字节)(数据[IDX] - 'A');
}
// 4字节至3个字节的转换
INT SIDX,didx;
为(SIDX = 0,didx = 0; didx&所述; dest.length-2; SIDX + = 4,didx + = 3)
{
DEST [didx] =(字节)(((数据[SIDX]其中; 2)及255)|
((数据[SIDX + 1] GT;>&→4)和3));
目标寄存器[didx + 1] =(字节)(((数据[SIDX + 1]; 4;)及255)|
((数据[SIDX + 2]≥>→2)及017));
目标寄存器[didx + 2] =(字节)(((数据[SIDX + 2]其中;&10 6)及255)|
(数据[SIDX + 3]&安培; 077));
}
如果(didx< dest.length)
{
DEST [didx] =(字节)(((数据[SIDX]其中; 2)及255)|
((数据[SIDX + 1] GT;>&→4)和3));
}
如果(++ didx< dest.length)
{
DEST [didx] =(字节)(((数据[SIDX + 1]; 4;)及255)|
((数据[SIDX + 2]≥>→2)及017));
}
返回DEST;
}
/ **
*一个简单的测试连接codeS和德codeS的第一个命令行参数。
* /
公共静态最终无效的主要(字串[] args)
{
如果(args.length!= 1)
{
的System.out.println(用法:Base64的字符串);
System.exit(0);
}
尝试
{
串E = Base64.en code(参数[0] .getBytes());
串D =新的String(Base64.de code(E));
的System.out.println(输入='+的args [0] +');
的System.out.println(恩codeD ='+ E +');
的System.out.println(德codeD ='+ D +');
}
赶上(例外X)
{
x.printStackTrace();
}
}
}
首先将您的位图的字节数组按照code:
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
image.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
byte []的B = baos.toByteArray();
要使用[]位图图像的这个呼叫传递字节:
base64Image = Base64.en code(B));
您可以通过点击下面一行在你的浏览器中检查它,你会看到图像。
数据:为image / jpeg; BASE64,base64Image
I am working on an application in which I need to capture an Image from camera. After capture, I have to convert the Bitmap to Base64. After converting to Base64, I have to send that String to SERVER. I am using below code for this task:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
base64Image = Base64.encodeToString(b,Base64.DEFAULT);
Problem : When I convert that Base64 to image, I am getting INCOMPLETE IMAGE. The same result is happening over server where my image is not perfectly re-constructed from Base64 String.
Please suggest me the solution. I have already search a-lot and getting same code which I am using right now.
Edited: please see the below incomplete Image
Code use to capture the Image:
intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PHOTO);
Create your own Base64
class and use this :
public class Base64
{
private Base64()
{
super();
}
/**
* Encode some data and return a String.
*/
public final static String encode(byte[] d)
{
if (d == null) return null;
byte data[] = new byte[d.length+2];
System.arraycopy(d, 0, data, 0, d.length);
byte dest[] = new byte[(data.length/3)*4];
// 3-byte to 4-byte conversion
for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4)
{
dest[didx] = (byte) ((data[sidx] >>> 2) & 077);
dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 |
(data[sidx] << 4) & 077);
dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 |
(data[sidx+1] << 2) & 077);
dest[didx+3] = (byte) (data[sidx+2] & 077);
}
// 0-63 to ascii printable conversion
for (int idx = 0; idx <dest.length; idx++)
{
if (dest[idx] < 26) dest[idx] = (byte)(dest[idx] + 'A');
else if (dest[idx] < 52) dest[idx] = (byte)(dest[idx] + 'a' - 26);
else if (dest[idx] < 62) dest[idx] = (byte)(dest[idx] + '0' - 52);
else if (dest[idx] < 63) dest[idx] = (byte)'+';
else dest[idx] = (byte)'/';
}
// add padding
for (int idx = dest.length-1; idx > (d.length*4)/3; idx--)
{
dest[idx] = (byte)'=';
}
return new String(dest);
}
/**
* Encode a String using Base64 using the default platform encoding
**/
public final static String encode(String s) {
return encode(s.getBytes());
}
/**
* Decode data and return bytes.
*/
public final static byte[] decode(String str)
{
if (str == null) return null;
byte data[] = str.getBytes();
return decode(data);
}
/**
* Decode data and return bytes. Assumes that the data passed
* in is ASCII text.
*/
public final static byte[] decode(byte[] data)
{
int tail = data.length;
while (data[tail-1] == '=') tail--;
byte dest[] = new byte[tail - data.length/4];
// ascii printable to 0-63 conversion
for (int idx = 0; idx <data.length; idx++)
{
if (data[idx] == '=') data[idx] = 0;
else if (data[idx] == '/') data[idx] = 63;
else if (data[idx] == '+') data[idx] = 62;
else if (data[idx] >= '0' && data[idx] <= '9')
data[idx] = (byte)(data[idx] - ('0' - 52));
else if (data[idx] >= 'a' && data[idx] <= 'z')
data[idx] = (byte)(data[idx] - ('a' - 26));
else if (data[idx] >= 'A' && data[idx] <= 'Z')
data[idx] = (byte)(data[idx] - 'A');
}
// 4-byte to 3-byte conversion
int sidx, didx;
for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3)
{
dest[didx] = (byte) ( ((data[sidx] << 2) & 255) |
((data[sidx+1] >>> 4) & 3) );
dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) |
((data[sidx+2] >>> 2) & 017) );
dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) |
(data[sidx+3] & 077) );
}
if (didx < dest.length)
{
dest[didx] = (byte) ( ((data[sidx] << 2) & 255) |
((data[sidx+1] >>> 4) & 3) );
}
if (++didx < dest.length)
{
dest[didx] = (byte) ( ((data[sidx+1] << 4) & 255) |
((data[sidx+2] >>> 2) & 017) );
}
return dest;
}
/**
* A simple test that encodes and decodes the first commandline argument.
*/
public static final void main(String[] args)
{
if (args.length != 1)
{
System.out.println("Usage: Base64 string");
System.exit(0);
}
try
{
String e = Base64.encode(args[0].getBytes());
String d = new String(Base64.decode(e));
System.out.println("Input = '" + args[0] + "'");
System.out.println("Encoded = '" + e + "'");
System.out.println("Decoded = '" + d + "'");
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
First Convert your bitmap to byteArray by following code :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
To use this call pass byte[] of your bitmap image:
base64Image = Base64.encode(b));
You can check it by hitting following line in your browser, you will see image.
data:image/jpeg;base64,base64Image
这篇关于不能够转换位图,以完善的Base64字符串中的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!