2011-12-06 84 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” –

相关问题