使用 ksoap2 将图像从 Android 发送到 Web 服务
我想使用用相机捕获的 ksoap2 库图像发送图像:
public class testwebservice extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String SOAP_ACTION="http://axis2.soap.webservice.vogella.de/getNumberResponse";
private static final String METHOD_NAME="getNumberResponse";
private static final String NAMESPACE="http://axis2.soap.webservice.vogella.de/";
private static final String URL="http://192.168.0.178:8080/Randomnumber/services/RandomNumber.RandomNumberHttpEndpoint/";
ImageView imv;
TextView tv;
Bitmap bmp;
int i;
final static int CAMERA_RESULT = 0;
Button upload;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.text1);
upload = (Button)findViewById(R.id.upload);
upload.setOnClickListener(this);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
Bundle extras = intent.getExtras();
bmp = (Bitmap) extras.get("data");
imv = (ImageView) findViewById(R.id.ReturnedImageView);
imv.setImageBitmap(bmp);
}
}
}
然后我有一个 AlertDialog
它询问我是否要发送图像,然后我调用转换图像的实际函数使用Marshallbase64:
public void testWebService() {
MarshalBase64 b = new MarshalBase64();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, out);
byte[] imagebyte = out.toByteArray();
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("image",imagebyte);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet=true;
soapEnvelope.setOutputSoapObject(Request);
b.register(soapEnvelope);
HttpTransportSE aht=new HttpTransportSE(URL);
try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse();
tv.setText("REZULTAT:"+response);
}
catch(Exception e)
{
e.printStackTrace();
}
}
我使用Eclipse WTP和Axis2创建了一个Web服务,这是一个返回随机数的简单方法:
package de.vogella.webservice.soap.axis2;
import java.util.Random;
public class RandomNumber {
public float[] getNumber(byte[] image){
float u[] = new float[8];
for(int i = 0; i < 8; i++) {
u[i] = new Float(i);
Random random = new Random();
u[i]= random.nextFloat();
}
return u;
}
}
我需要做的就是链接这三个,我的工作就完成了。有人可以帮忙吗?
I want to send an image using ksoap2 library image captured with a camera:
public class testwebservice extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String SOAP_ACTION="http://axis2.soap.webservice.vogella.de/getNumberResponse";
private static final String METHOD_NAME="getNumberResponse";
private static final String NAMESPACE="http://axis2.soap.webservice.vogella.de/";
private static final String URL="http://192.168.0.178:8080/Randomnumber/services/RandomNumber.RandomNumberHttpEndpoint/";
ImageView imv;
TextView tv;
Bitmap bmp;
int i;
final static int CAMERA_RESULT = 0;
Button upload;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.text1);
upload = (Button)findViewById(R.id.upload);
upload.setOnClickListener(this);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
Bundle extras = intent.getExtras();
bmp = (Bitmap) extras.get("data");
imv = (ImageView) findViewById(R.id.ReturnedImageView);
imv.setImageBitmap(bmp);
}
}
}
Then I have an AlertDialog
which asks me if I want to send the image or not, then I call the actual function that transforms the image using Marshallbase64
:
public void testWebService() {
MarshalBase64 b = new MarshalBase64();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, out);
byte[] imagebyte = out.toByteArray();
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("image",imagebyte);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet=true;
soapEnvelope.setOutputSoapObject(Request);
b.register(soapEnvelope);
HttpTransportSE aht=new HttpTransportSE(URL);
try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse();
tv.setText("REZULTAT:"+response);
}
catch(Exception e)
{
e.printStackTrace();
}
}
I made a webservice using Eclipse WTP and Axis2 it is a simple method which returns random numbers:
package de.vogella.webservice.soap.axis2;
import java.util.Random;
public class RandomNumber {
public float[] getNumber(byte[] image){
float u[] = new float[8];
for(int i = 0; i < 8; i++) {
u[i] = new Float(i);
Random random = new Random();
u[i]= random.nextFloat();
}
return u;
}
}
All I need to do is to link this three and my work is done. Can someone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以继续使用代码,而不是进行封送:
在您的 webmethod 中解码字符串,如下所示:
Instead of marshaling you can continue with your code:
In your webmethod decode the string as below:
从您的问题中不清楚您到底将图像存储在哪里。您显示的 Web 服务代码仅返回一个数字,不支持 HTTP POST 请求。在我看来,从你的代码来看,你需要调整你的网络服务来接受这种类型的请求并将你的编组字节数组存储在数据库中。因此,下一个逻辑步骤是创建一个数据库来存储这些图像,然后更新您的 Web 服务以连接到该数据库并插入图像数据。最后,您的 Android 应用程序将调用图像编组代码,然后使用生成的字节数组进行 Web 服务调用。
It's not clear from your question where exactly you are storing the image. The webservice code you have shown just returns a number and does not support HTTP POST requests. It seems to me from your code that you need to adjust your webservice to accept this type of request and store your marshalled byte array in a database. So the next logical step would be to create a database for storing these images, and then updating your webservice to connect to this DB and insert the image data. Finally your Android app would call the image marshalling code and then make the webservice call with the resultant byte array.