1、原理
Android拍照将图片上传至服务器,其过程可以分为,1:将拍照的图片以64位的字节流形式存在;2:发布接收64位字节流并将字节流转换成图片的方法;3、访问发布的WebServer,实现图片的上传。
2、图片转为64位字节流
1、选择相机拍照和本地相册
调用相机拍照
paizhao.setOnClickListener(new paizhaoOnclick());
bendi.setOnClickListener(new bendiOnclick());
public class paizhaoOnclick implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
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();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
图片转为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 "";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
3、服务器端接收
使用VS2013编写服务器接收64字节流文件并转换成图片存储的格式PNG、JPG等。
[WebService(Namespace = "http://***.***.***.**/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string UpImage(String dataURL,String path,String imgname)
{
String filename = "";
String base64 = dataURL.Substring(dataURL.IndexOf(",")+1);
System.Drawing.Bitmap bitmap = null;
try
{
String inputString = base64;
byte[] arr = Convert.FromBase64String(inputString);
System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
4、将方法发布成WebServer
这里就不详细讲述WebServer发布的过程,大家可以参考
发布WebServer教程的链接
。
5、调用发布的服务
使用KSoap2调用上传照片的方法。
public class shangchuanOnclick implements View.OnClickListener{
@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);
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
6、上传图片结果
运行程序之后会在服务器端相应的文件夹下存储上传的图片