繁体   English   中英

将图像从Android发送到C#Web服务

[英]Sending Image From Android to C# webservice

我正在将图像从Android发送到C#Web服务。 我总是得到黑色图像。有人可以帮忙吗?

Android方面:

 public void save(View v) {           
        mBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);           
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG,40, outputStream);
        byte[] imgByte = outputStream.toByteArray();
        String base64Str = Base64.encodeToString(imgByte, Base64.DEFAULT);
       // Send base64Str to server
}

C#端:

[WebMethod]
public void GetImage(string base64ImageStr)
{           
      byte[] imageBytes = Convert.FromBase64String(base64ImageStr);
      //Save imageBytes to DB                
 }

我从数据库中调用它:

 public ActionResult Image(){
        var bytes=GetBytesArrayFromDB(id);
        return File(bytes, "image/jpeg");
    }

在视图中:

<img src='@Url.Action("Image")' alt="" />

我发现在将其发送到服务器之前我没有对其进行画布处理。 也用这个

Canvas canvas = new Canvas(mBitmap); v.draw(帆布);

 public void save(View v) {           
            mBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);           
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            mBitmap.compress(Bitmap.CompressFormat.JPEG,40, outputStream);
            byte[] imgByte = outputStream.toByteArray();
            String base64Str = Base64.encodeToString(imgByte, Base64.DEFAULT);

            Canvas canvas = new Canvas(mBitmap);
            v.draw(canvas);

           // Send base64Str to server
    }

//将base64Str发送到服务器

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class CallSoap
{
    public final String SOAP_ACTION = //url;
    public  final String OPERATION_NAME = "name of action";
    public  final String WSDL_TARGET_NAMESPACE = "asmx url";
    public  final String SOAP_ADDRESS = "soap address";
public CallSoap()
{
}
public String Call(String base64Str)
{
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

    PropertyInfo pi=new PropertyInfo();
    pi.setName("base64Str");
    pi.setValue(base64Str);
    pi.setType(String.class);
    request.addProperty(pi);

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

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;
    try
    {
        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
    }
    catch (Exception exception)
    {
        response=exception.toString();
    }
    return response.toString();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM