一、原理html
Android拍照將圖片上傳至服務器,其過程能夠分爲,1:將拍照的圖片以64位的字節流形式存在;2:發佈接收64位字節流並將字節流轉換成圖片的方法;三、訪問發佈的WebServer,實現圖片的上傳。android
二、圖片轉爲64位字節流數組
一、選擇相機拍照和本地相冊服務器
調用相機拍照ide
paizhao.setOnClickListener(new paizhaoOnclick());url
bendi.setOnClickListener(new bendiOnclick());spa
public class paizhaoOnclick implements View.OnClickListener{debug
@Overridecode
public void onClick(View v) {orm
//Intent intent =new Intent("android.media.action.IMAGE_CAPTURE");
Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);
startActivityForResult(intent, 1000);
}
}
選擇本地相冊
public class bendiOnclick implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop",true);
intent.putExtra("return-data",true);
startActivityForResult(intent,1001);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1000&&resultCode==RESULT_OK){
String sdStatus= Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED))
{
return;
}
Bundle bundle=data.getExtras();
Bitmap bitmap=(Bitmap)bundle.get("data");
FileOutputStream b=null;
File file=new File("/sdcard/myImage/");
file.mkdirs();
String str=null;
Date date=null;
SimpleDateFormat format =new SimpleDateFormat("yyyyMMddHHmmss");
date=new Date();
str=format.format(date);
filename="/sdcard/myImage/"+str+".png";
try{
b=new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.PNG,100,b);
}
catch(Exception e)
{
}
finally {
try
{
b.flush();
b.close();
}
catch(Exception e)
{
}
if(data!=null)
{
Bundle extras=data.getExtras();
Bitmap camerabitmap=(Bitmap)extras.get("data");
imageView.setImageBitmap(camerabitmap);
}
}
}
else if (requestCode==1001&&resultCode==RESULT_OK){
Uri uri= data.getData();
//獲取遊標
ContentResolver resolver =getContentResolver();
try {
Bitmap bitmap= BitmapFactory.decodeStream(resolver.openInputStream(uri));
imageView.setImageBitmap(bitmap);
}
catch (Exception e){
e.printStackTrace();
}
}
//將圖片轉爲字節流
}
圖片轉爲64位字節流的方法
public String picturezijie(String filename){
byte[]byte1=null;
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
try{
FileInputStream fileInputStream=new FileInputStream(filename);
byte[]buttfer=new byte[1024*10*1024];
int len=0;
while ((len=fileInputStream.read(buttfer))>=0)
{
byteArrayOutputStream.write(buttfer,0,len);
}
byte1=byteArrayOutputStream.toByteArray();
String imagebuffer= Base64.encodeToString(byte1,Base64.NO_WRAP);
byteArrayOutputStream.close();
fileInputStream.close();
return imagebuffer;
}
catch (Exception e)
{
return "";
}
}
三、服務器端接收
使用VS2013編寫服務器接收64字節流文件並轉換成圖片存儲的格式PNG、JPG等。
/// <summary>
/// WebService1 的摘要說明
/// </summary>
[WebService(Namespace = "http://***.***.***.**/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要容許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消註釋如下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string UpImage(String dataURL,String path,String imgname)
{
//dataURL base64數據
//path 保存路徑
//imgname圖片名字
//string類型的相對路徑
String filename = "";
//將,之前的多餘字符串刪除
String base64 = dataURL.Substring(dataURL.IndexOf(",")+1);
//定義一個Bitmap對象,接受轉換完成的圖片
System.Drawing.Bitmap bitmap = null;
try
{
//把純淨的Base64的資源給Inputstring
String inputString = base64;
//將Base64轉換成等效的8位的無符號整形數組
byte[] arr = Convert.FromBase64String(inputString);
//轉換成沒法調整大小的MemoryStream對象
System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
//將MemoryStream對象轉換成Bitmap對象
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
//關閉資源
ms.Close();
bitmap = bmp;
//所要保存的相對路徑及名字
filename = path + "/TJH_" + imgname + ".png";
//獲取程序根目錄
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());
//轉換成絕對路徑
string imagesurl2 = tmpRootDir + filename.Replace(@"/", @"\");
//保存到服務器路徑
bitmap.Save(imagesurl2, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception) {
}
//返回相對路徑
return filename;
}
}
四、將方法發佈成WebServer
這裏就不詳細講述WebServer發佈的過程,你們能夠參考發佈
五、調用發佈的服務
使用KSoap2調用上傳照片的方法。
@Override
public void onClick(View v) {
String bytereturn=picturezijie(filename);
System.out.println("64位的字節流---------》》》》"+ bytereturn);
String filename1="486";
String path=null;
SoapObject request = new SoapObject(NAMESPACE, METHON_NAME);
request.addProperty("dataURL", bytereturn);
function(){ //外匯點差http://www.kaifx.cn/question/kaifx/1765.html
request.addProperty("path", path);
request.addProperty("imgname", filename1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.debug = true;
try
{
httpTransportSE.call(SOAP_ACTION, envelope);
}
catch (Exception e)
{
e.printStackTrace();
}
SoapObject object = (SoapObject) envelope.bodyIn;
String result = object.getProperty(0).toString();
System.out.println("上傳輸出------------》》》"+result);
Toast.makeText(getApplicationContext(),"上傳成功",Toast.LENGTH_SHORT).show();
}
}