Android上使用camera拍照,把获取的照片上传到远程服务器

url: http://blog.csdn.net/jdsjlzx/article/details/42027993


Android上使用camera拍照,把获取的照片上传到远程服务器_第1张图片Android上使用camera拍照,把获取的照片上传到远程服务器_第2张图片

使用Java上传文件

从Apache Software Foundation下载HttpClient 4.3.4。

在工程中添加下面的jar包:

Android上使用camera拍照,把获取的照片上传到远程服务器_第3张图片

参考sample,写一个简单的上传:

?
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
public  static  void  main(String[] args)  throws  Exception {
         // TODO Auto-generated method stub
         CloseableHttpClient httpclient = HttpClients.createDefault();
         try  {
             HttpPost httppost =  new  HttpPost( "http://localhost:8003/savetofile.php" ); // your server
  
             FileBody bin =  new  FileBody( new  File( "my.jpg" ));  // image for uploading
  
             HttpEntity reqEntity = MultipartEntityBuilder.create()
                     .addPart( "myFile" , bin)
                     .build();
  
             httppost.setEntity(reqEntity);
  
             System.out.println( "executing request "  + httppost.getRequestLine());
             CloseableHttpResponse response = httpclient.execute(httppost);
             try  {
                 System.out.println( "----------------------------------------" );
                 System.out.println(response.getStatusLine());
                 HttpEntity resEntity = response.getEntity();
                 if  (resEntity !=  null ) {
                     System.out.println( "Response content length: "  + resEntity.getContentLength());
                 }
                 EntityUtils.consume(resEntity);
             finally  {
                 response.close();
             }
         finally  {
             httpclient.close();
         }
     }

Android上拍照

Camera的使用很简单,只需要参考开发者网站的这篇Taking Photos Simply。

调用系统camera只需要如下代码:

?
1
2
Intent intent =  new  Intent( "android.media.action.IMAGE_CAPTURE" );
startActivityForResult(intent,  0 );
 

拍照之后,camera会返回缩略图:

?
1
2
3
4
5
6
7
protected  void  onActivityResult( int  requestCode,  int  resultCode, Intent data) {
     if  (requestCode ==  0  && resultCode == RESULT_OK) {
         Bundle extras = data.getExtras();
         Bitmap imageBitmap = (Bitmap) extras.get( "data" );
         mImageView.setImageBitmap(imageBitmap);
     }
}
 

如果要获得高质量的图,就需要指定照片的保存路径。在AndroidManifest.xml中添加下面的权限:

?
1
2
"android.permission.READ_EXTERNAL_STORAGE"  />
"android.permission.WRITE_EXTERNAL_STORAGE"  />
 

修改调用方法:

?
1
2
3
Intent intent =  new  Intent( "android.media.action.IMAGE_CAPTURE" );
Intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(intent,  0 );
 

拍照之后,使用预设的图片路径解码,就可以获取高质量的图:

?
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
protected  void  onActivityResult( int  requestCode,  int  resultCode, Intent data) {
         if  (requestCode ==  0  && resultCode == Activity.RESULT_OK) {
             setPic();
         }
}
  
private  void  setPic() {
     // Get the dimensions of the View
     int  targetW = mImageView.getWidth();
     int  targetH = mImageView.getHeight();
  
     // Get the dimensions of the bitmap
     BitmapFactory.Options bmOptions =  new  BitmapFactory.Options();
     bmOptions.inJustDecodeBounds =  true ;
     BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
     int  photoW = bmOptions.outWidth;
     int  photoH = bmOptions.outHeight;
  
     // Determine how much to scale down the image
     int  scaleFactor = Math.min(photoW/targetW, photoH/targetH);
  
     // Decode the image file into a Bitmap sized to fill the View
     bmOptions.inJustDecodeBounds =  false ;
     bmOptions.inSampleSize = scaleFactor;
     bmOptions.inPurgeable =  true ;
  
     Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
     mImageView.setImageBitmap(bitmap);
}
 

从Android客户端传送图片到PHP服务器

要访问Internet,在AndroidManifest.xml中添加访问权限:

?
1
"android.permission.INTERNET" />

参考http://blog.rafaelsanches.com/2011/01/29/upload-using-multipart-post-using-httpclient-in-android/,创建一个类MultipartEntity:

?
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
public  class  MultipartEntity  implements  HttpEntity {
  
     private  String boundary =  null ;
  
     ByteArrayOutputStream out =  new  ByteArrayOutputStream();
     boolean  isSetLast =  false ;
     boolean  isSetFirst =  false ;
  
     public  MultipartEntity() {
         this .boundary = System.currentTimeMillis() +  "" ;
     }
  
     public  void  writeFirstBoundaryIfNeeds(){
         if (!isSetFirst){
             try  {
                 out.write(( "--"  + boundary +  "\r\n" ).getBytes());
             catch  ( final  IOException e) {
  
             }
         }
         isSetFirst =  true ;
     }
  
     public  void  writeLastBoundaryIfNeeds() {
         if (isSetLast){
             return  ;
         }
         try  {
             out.write(( "\r\n--"  + boundary +  "--\r\n" ).getBytes());
         catch  ( final  IOException e) {
  
         }
         isSetLast =  true ;
     }
  
     public  void  addPart( final  String key,  final  String value) {
         writeFirstBoundaryIfNeeds();
         try  {
             out.write(( "Content-Disposition: form-data; name=\""  +key+ "\"\r\n" ).getBytes());
             out.write( "Content-Type: text/plain; charset=UTF-8\r\n" .getBytes());
             out.write( "Content-Transfer-Encoding: 8bit\r\n\r\n" .getBytes());
             out.write(value.getBytes());
             out.write(( "\r\n--"  + boundary +  "\r\n" ).getBytes());
         catch  ( final  IOException e) {
  
         }
     }
  
     public  void  addPart( final  String key,  final  String fileName,  final  InputStream fin){
         addPart(key, fileName, fin,  "application/octet-stream" );
     }
  
     public  void  addPart( final  String key,  final  String fileName,  final  InputStream fin, String type){
         writeFirstBoundaryIfNeeds();
         try  {
             type =  "Content-Type: " +type+ "\r\n" ;
             out.write(( "Content-Disposition: form-data; name=\"" + key+ "\"; filename=\""  + fileName +  "\"\r\n" ).getBytes());
             out.write(type.getBytes());
             out.write( "Content-Transfer-Encoding: binary\r\n\r\n" .getBytes());
  
             final  byte [] tmp =  new  byte [ 4096 ];
             int  l =  0 ;
             while  ((l = fin.read(tmp)) != - 1 ) {
                 out.write(tmp,  0 , l);
             }
             out.flush();
         catch  ( final  IOException e) {
  
         finally  {
             try  {
                 fin.close();
             catch  ( final  IOException e) {
  
             }
         }
     }
  
     public  void  addPart( final  String key,  final  File value) {
         try  {
             addPart(key, value.getName(),  new  FileInputStream(value));
         catch  ( final  FileNotFoundException e) {
  
         }
     }
  
     @Override
     public  long  getContentLength() {
         writeLastBoundaryIfNeeds();
         return  out.toByteArray().length;
     }
  
     @Override
     public  Header getContentType() {
         return  new  BasicHeader( "Content-Type" "multipart/form-data; boundary="  + boundary);
     }
  
     @Override
     public  boolean  isChunked() {
         return  false ;
     }
  
     @Override
     public  boolean  isRepeatable() {
         return  false ;
     }
  
     @Override
     public  boolean  isStreaming() {
         return  false ;
     }
  
     @Override
     public  void  writeTo( final  OutputStream outstream)  throws  IOException {
         outstream.write(out.toByteArray());
     }
  
     @Override
     public  Header getContentEncoding() {
         return  null ;
     }
  
     @Override
     public  void  consumeContent()  throws  IOException,
     UnsupportedOperationException {
         if  (isStreaming()) {
             throw  new  UnsupportedOperationException(
             "Streaming entity does not implement #consumeContent()" );
         }
     }
  
     @Override
     public  InputStream getContent()  throws  IOException,
     UnsupportedOperationException {
         return  new  ByteArrayInputStream(out.toByteArray());
     }
  
}
 

使用AsyncTask来完成上传:

?
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
66
67
68
69
70
private  class  UploadTask  extends  AsyncTask {
  
         protected  Void doInBackground(Bitmap... bitmaps) {
             if  (bitmaps[ 0 ] ==  null )
                 return  null ;
  
             Bitmap bitmap = bitmaps[ 0 ];
             ByteArrayOutputStream stream =  new  ByteArrayOutputStream();
             bitmap.compress(Bitmap.CompressFormat.PNG,  100 , stream);  // convert Bitmap to ByteArrayOutputStream
             InputStream in =  new  ByteArrayInputStream(stream.toByteArray());  // convert ByteArrayOutputStream to ByteArrayInputStream
  
             DefaultHttpClient httpclient =  new  DefaultHttpClient();
             try  {
                 HttpPost httppost =  new  HttpPost(
                         "http://192.168.8.84:8003/savetofile.php" ); // server
  
                 MultipartEntity reqEntity =  new  MultipartEntity();
                 reqEntity.addPart( "myFile" ,
                         System.currentTimeMillis() +  ".jpg" , in);
                 httppost.setEntity(reqEntity);
  
                 Log.i(TAG,  "request "  + httppost.getRequestLine());
                 HttpResponse response =  null ;
                 try  {
                     response = httpclient.execute(httppost);
                 catch  (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 catch  (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 try  {
                     if  (response !=  null )
                         Log.i(TAG,  "response "  + response.getStatusLine().toString());
                 finally  {
  
                 }
             finally  {
  
             }
  
             if  (in !=  null ) {
                 try  {
                     in.close();
                 catch  (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
  
             if  (stream !=  null ) {
                 try  {
                     stream.close();
                 catch  (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
  
             return  null ;
         }
  
         @Override
         protected  void  onPostExecute(Void result) {
             // TODO Auto-generated method stub
             super .onPostExecute(result);
             Toast.makeText(MainActivity. this , R.string.uploaded, Toast.LENGTH_LONG).show();
         }
     }

代码

https://github.com/DynamsoftRD/JavaHTTPUpload

?
1
Git clone https: //github.com/DynamsoftRD/JavaHTTPUpload.git

你可能感兴趣的:(android_camera)