2011-12-06 86 views
0

我正在嘗試構建一個從相機中獲取圖像的Android應用程序。然後我將該圖像發送到我構建和編輯它的Web服務。在這一刻,我只是試圖發送圖像,然後將其恢復並顯示在imageView中。我只是想看看如何使用Web服務。我正在發送byte []並接收byte []。我是否必須對Web服務進行轉換並返回不同的類型,或者可以返回byte []?我使用kso​​ap2來連接網絡服務。當我得到結果以及如何將結果轉換爲位圖時,我不知道該怎麼做!任何幫助???????將圖像從Android上傳到網絡服務

代碼:

if (requestCode == CAMERA_DATA) 
     if (resultCode == Activity.RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      bitmap = (Bitmap) extras.get("data"); 

      setContentView(R.layout.photo3); 
      image = (ImageView) findViewById (R.id.imageView); 

      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      bitmap.compress(CompressFormat.JPEG, 100, out); 
      data1 = out.toByteArray(); 

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      request.addProperty("name",data1); 


      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = false; 
      envelope.setOutputSoapObject(request); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

       try { 
       androidHttpTransport.call(SOAP_ACTION, envelope); 

       SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse(); 

       //WHAT TO DO HERE 

       image.setImageBitmap(btm); 



       } catch (Exception e) { 


       } 






      try { 
       out.close(); 
      } 
      catch(IOException e){ 

      } 
    } 
+0

使用下面的鏈接:(非常有助於你發送圖像通過Http) http://stackoverflow.com/questions/2935946/sending-images-using-http-post – Maneesh

+0

@Nana使用FTP也可以,如果你想然後我準備好幫助 –

+0

上傳圖像使用base64轉換器將圖像轉換爲基本64格式 在服務器端使用base 64來解碼該文件 –

回答

0

什麼是字節數組代表什麼?這是一個特定格式的文件嗎? (PNG,JPEG等)還是原始字節代表圖像的RGB(A)值? 在最後一種情況下,標準Java庫的BufferedImage可能會有幫助。 尤其是setRGB方法:http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html#setRGB(int,%20int,%20int,%20int,%20int[],%20int,%20int)

只需實例所需的尺寸和正確的圖像類型的新的BufferedImage並使用setRGB方法中的像素值進行粘貼。 這可以稍後用於在ImageView中顯示或簡單地保存在設備上。

我希望這有助於...

+0

Maneesh的帖子的BitmapFactory也是一個很好的起點:https://developer.android.com/reference/android/graphics/BitmapFactory.html – kraenhansen

1

你好,請檢查下面

第一種方法的代碼被用於文件轉換的=爲base64和第二種方法是壓縮任何圖像。您可以使用這些代碼來編碼爲Base64和添加是從第一種方法返回的肥皂參數字符串

private String getEncodeData(String filePath) { 
      String encodedimage1 = null; 
      if (filePath != null && filePath.length() > 0) { 
       try { 
        Bitmap bm = decodeFile(new File (filePath)); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        bm.compress(Bitmap.CompressFormat.PNG, 50, baos); //bm is the bitmap object 
        byte[] b = baos.toByteArray(); 
        encodedimage1= Base64.encodeToString(b, Base64.DEFAULT); 
       } catch (Exception e) { 
        System.out 
        .println("Exception: In getEncodeData" + e.toString()); 
       } 
      } 
      return encodedimage1; 
     } 

private Bitmap decodeFile(File f){ 
     Bitmap b = null; 
     final int IMAGE_MAX_SIZE = 100; 
     try { 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      FileInputStream fis = new FileInputStream(f); 
      BitmapFactory.decodeStream(fis, null, o); 
      fis.close(); 
      int scale = 1; 
      if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { 
       scale = (int) Math.pow(2.0, (int) Math.round(Math.log(IMAGE_MAX_SIZE/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5))); 
      } 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      fis = new FileInputStream(f); 
      b = BitmapFactory.decodeStream(fis, null, o2); 
      fis.close(); 
     } catch (Exception e) { 
      Log.v("Exception in decodeFile() ",e.toString()+""); 
     } 
     return b; 
    } 

請讓我知道任何更多的困難

+0

返回一行中的錯誤:encodedimage1 = Base64.encodeToString(b,Base64。默認); – Katerina

+0

@ Abhinav Singh Maurya在getEncodeData(String filePath)我如何獲得照片的filePath並將其提供給該方法...我正在使用真實設備來運行我的應用程序.. – Katerina

+0

您可以從它的位置獲取文件路徑使用Environment.getExternalStorageDirectory()存儲;這會給你你的SD卡的路徑和你的文件保存的路徑給它的EX: Environment.getExternalStorageDirectory()+「/ MyFolder/Imagename.jpg」 –

相關問題