Android 实现图片转二进制流及二进制转字符串
我就废话不多说了,还是直接看代码吧
publicstaticbyte[]readStream(Stringimagepath)throwsException{
FileInputStreamfs=newFileInputStream(imagepath);
ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
while(-1!=(len=fs.read(buffer))){
outStream.write(buffer,0,len);
}
outStream.close();
fs.close();
returnoutStream.toByteArray();
}
//二进制转字符串
publicstaticStringbyte2hex(byte[]b)
{
StringBuffersb=newStringBuffer();
Stringtmp="";
for(inti=0;i
补充知识:图片(bitmap)转化为byte[],并上传到服务器
压缩为3:1bitmap
/**
*压缩bitmap
*
*@parambitmap
*@return
*/
privateBitmapcompressBitmap(Bitmapbitmap){
//创建操作图片用的matrix对象
Matrixmatrix=newMatrix();
matrix.postScale((float)300/bitmap.getWidth(),
(float)100/bitmap.getHeight());
Log.e("","bitmap_width"+bitmap.getWidth());
Log.e("","bitmap_height"+bitmap.getHeight());
BitmapcompressBitmap=Bitmap.createBitmap(bitmap,0,0,
bitmap.getWidth(),bitmap.getHeight(),matrix,true);
returncompressBitmap;
}
上传压缩后的bitmap
privatevoideleSignature(){
WritePadDialogwriteTabletDialog=newWritePadDialog(
this,newDialogListener(){
publicvoidrefreshActivity(Objectobject){
BitmapmSignBitmap=(Bitmap)object;
mSignBitmap=compressBitmap(mSignBitmap);
//BitmapnewsrcImage=mSignBitmap.copy(Bitmap.Config.RGB_565,true);
saveBmp(mSignBitmap);
Log.e("","afterbitmap_width:"+mSignBitmap.getWidth());
Log.e("","afterbitmap_height:"+mSignBitmap.getHeight());
Filefile=newFile(file_name);
Log.e("Jeny","file.exists():"+file.exists());
if(!file.exists()){
return;
}
byte[]eleSignatureData=null;
try{
eleSignatureData=InputStreamTOByte();
}catch(IOExceptione){
}
Log.e("Jeny","eleSignatureData:"+eleSignatureData.length);
if(eleSignatureData==null){
return;
}
//byte[]eleSignatureData=InputStreamTOByte();
//byte[]eleSignatureData=Bitmap2Bytes(newsrcImage);
mController.startSavingElecSignatureDataOfSDPay(SalesCollectActivity.this,voucherNo,eleSignatureData);
mSignBitmap.recycle();
}
}
);
writeTabletDialog.setCanceledOnTouchOutside(false);
writeTabletDialog.setOnKeyListener(newDialogInterface.OnKeyListener(){
@Override
publicbooleanonKey(DialogInterfacedialogInterface,intkeyCode,KeyEventkeyEvent){
if(keyCode==KeyEvent.KEYCODE_BACK){
returntrue;
}else{
returnfalse;//默认返回false
}
}
});
writeTabletDialog.show();
}
/**
*将InputStream转换成byte数组
*
*InputStream
*
*@returnbyte[]
*@throwsIOException
*/
@SuppressWarnings("resource")
publicbyte[]InputStreamTOByte()throwsIOException{
Filefile=newFile(file_name);
if(!file.exists()){
returnnull;
}
FileInputStreamis=newFileInputStream(file_name);
ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
byte[]data=newbyte[BUFFER_SIZE];
intcount=-1;
while((count=is.read(data,0,BUFFER_SIZE))!=-1)
outStream.write(data,0,count);
//data=null;
byte[]alldata=outStream.toByteArray();
/**top=newbyte[62];
for(inti=0;i<62;i++)
{
top[i]=alldata[i];
}
writefile(top_name,top);
*/
//byte[]other=newbyte[alldata.length-62];
//for(inti=0;i<(alldata.length-62);i++)
//{
//other[i]=alldata[i+62];
//}
/*
*Stringb=
*Environment.getExternalStorageDirectory().getPath()+"/other.txt";
*writefile(b,other);
*/
returnalldata;
}
/**
*@parambitmap图片以bmp格式存储的方法
*/
privatevoidsaveBmp(Bitmapbitmap){
if(bitmap==null)
return;
//位图大小
intnBmpWidth=bitmap.getWidth();
intnBmpHeight=bitmap.getHeight();
intDataSizePerLine=40;
//图像数据大小
intbufferSize=nBmpHeight*DataSizePerLine;
try{
//存储文件名
Filefile=newFile(file_name);
Log.i("SurFace","filename:"+file_name);
if(!file.exists()){
file.createNewFile();
}
FileOutputStreamfileos=newFileOutputStream(file_name);
//bmp文件头
intbfType=0x4d42;
longbfSize=14+40+8+bufferSize;
intbfReserved1=0;
intbfReserved2=0;
longbfOffBits=14+40+8;
//保存bmp文件头
writeWord(fileos,bfType);
writeDword(fileos,bfSize);
writeWord(fileos,bfReserved1);
writeWord(fileos,bfReserved2);
writeDword(fileos,bfOffBits);
//bmp信息头
longbiSize=40L;
longbiWidth=nBmpWidth;
longbiHeight=nBmpHeight;
intbiPlanes=1;
intbiBitCount=1;
longbiCompression=0L;
longbiSizeImage=0L;
longbiXpelsPerMeter=0L;
longbiYPelsPerMeter=0L;
longbiClrUsed=0L;
longbiClrImportant=0L;
//保存bmp信息头
writeDword(fileos,biSize);
writeLong(fileos,biWidth);
writeLong(fileos,biHeight);
writeWord(fileos,biPlanes);
writeWord(fileos,biBitCount);
writeDword(fileos,biCompression);
writeDword(fileos,biSizeImage);
writeLong(fileos,biXpelsPerMeter);
writeLong(fileos,biYPelsPerMeter);
writeDword(fileos,biClrUsed);
writeDword(fileos,biClrImportant);
//调色板
byte[]c=newbyte[4];
c[0]=0;
c[1]=0;
c[2]=0;
c[3]=0;
fileos.write(c);
byte[]w=newbyte[4];
w[0]=(byte)255;
w[1]=(byte)255;
w[2]=(byte)255;
w[3]=0;
fileos.write(w);
intwWidth=DataSizePerLine;
//像素扫描
bytebmpData[]=newbyte[bufferSize];
for(intibuf=0;ibuf0;nCol--,nRealCol--){
wWidth=DataSizePerLine;
iCount=0;
iByte=0;
for(intwRow=0;wRow>16&0x00ff;
intg=Color.green(clr);
intb=Color.blue(clr);
//bytea=(byte)Color.blue(clr);
//byteb=(byte)Color.green(clr);
//byted=(byte)Color.red(clr);
//System.out.println("r:"+r);
if(r!=255||g!=255||b!=255)//黑色
{
//bPix=(byte)(bPix&(0x7f>>iByte|
//0x7f<<(8-iByte)));
bPix=(byte)(bPix&~(byte)Math.pow(2,iByte));
//System.out.println("黑色"+bPix);
}else{
//bPix=(byte)(bPix&(0xff>>iByte|
//0xff<<(8-iByte)));
bPix=(byte)(bPix|(byte)Math.pow(2,iByte));
//System.out.println("白色"+bPix);
}
if(iCount!=0&&iCount%8==0){
bmpData[nRealCol*wWidth-wRow/8]=bPix;
iByte=0;
}else{
iByte++;
}
iCount++;
/*
*//if(a==b&&b==d&&a>0)if(nRealCol%2==0){//为白色
*bmpData[nRealCol*wWidth]=(byte)255;}else{//为黑色
*bmpData[nRealCol*wWidth]=0;}
*/
//Arrays.fill(x,value);
/*
*bmpData[nRealCol*wWidth+wByteIdex]=(byte)Color.b;
*bmpData[nRealCol*wWidth+wByteIdex+1]=(byte)Color
*.green(clr);bmpData[nRealCol*wWidth+wByteIdex+2]=
*(byte)Color.red(clr);
*/
}
}
//System.out.println("iCount="+iCount);
/*
*byte[]writeByte=newbyte[bmpData.length];for(int
*i=0;i>8&0xff);
stream.write(b);
}
protectedvoidwriteDword(FileOutputStreamstream,longvalue)
throwsIOException{
byte[]b=newbyte[4];
b[0]=(byte)(value&0xff);
b[1]=(byte)(value>>8&0xff);
b[2]=(byte)(value>>16&0xff);
b[3]=(byte)(value>>24&0xff);
stream.write(b);
}
protectedvoidwriteLong(FileOutputStreamstream,longvalue)
throwsIOException{
byte[]b=newbyte[4];
b[0]=(byte)(value&0xff);
b[1]=(byte)(value>>8&0xff);
b[2]=(byte)(value>>16&0xff);
b[3]=(byte)(value>>24&0xff);
stream.write(b);
}
/**
*压缩bitmap
*
*@parambitmap
*@return
*/
privateBitmapcompressBitmap(Bitmapbitmap){
//创建操作图片用的matrix对象
Matrixmatrix=newMatrix();
matrix.postScale((float)300/bitmap.getWidth(),
(float)100/bitmap.getHeight());
Log.e("","bitmap_width"+bitmap.getWidth());
Log.e("","bitmap_height"+bitmap.getHeight());
BitmapcompressBitmap=Bitmap.createBitmap(bitmap,0,0,
bitmap.getWidth(),bitmap.getHeight(),matrix,true);
returncompressBitmap;
}
以上这篇Android实现图片转二进制流及二进制转字符串就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。