我正在尝试构建一个从相机中获取图像的Android应用程序。然后我将该图像发送到我构建和编辑它的Web服务。此刻我只是试图发送图像并将其取回并在imageView中显示。我只是想看看如何使用Web服务。我发送byte []并接收byte []。我是否必须转换为Web服务并返回其他类型,否则返回byte []?我使用ksoap2连接Web服务。当我得到结果以及如何将结果转换为位图时,我不知道该怎么办!任何帮助???????
代码:
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 :(得分:1)
您好请检查以下代码
第一种方法用于将文件in =转换为base64,第二种方法用于压缩任何图像。您可以使用这些代码编码到base64并添加从第一个方法返回的soap参数字符串
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;
}
请让我知道更多的困难
答案 1 :(得分: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中,或者只是保存在设备上。
我希望这会有所帮助......