java-将图像转换为字节数组,反之亦然

我正在尝试通过android将图像作为字节数组发送,并在服务器端将其转换回png文件,我在服务器端得到IllegalArgumentException.

这是我的android代码,它将图像转换为字节数组并将其作为请求发送到服务器:

 GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.signaturePad);

            Bitmap bm = Bitmap.createBitmap(gestureView.getGesture().toBitmap(100, 100, 8, Color.WHITE));

            FileOutputStream fos = openFileOutput("signature.png", Context.MODE_PRIVATE);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fos);

            byte[] image_byte = fos.toString().getBytes();//Base64.encode(fos.toString().getBytes(), 0);

            String strImage = image_byte.toString();

这是我的服务器端程序:

String imageString = request.getParameter("SignatureImage");
    byte[] imageByte = imageString.getBytes();
    try {
        InputStream in = new ByteArrayInputStream(imageByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);

        //Getting exception at this line
        ImageIO.write(bImageFromConvert, "png", new File(
                "E:/signature/signature.png"));
        /*BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageByte));
        ImageIO.write(bufferedImage, "jpg", new File("E:/signature/signature.jpg"));*/
    } catch (IOException e) {
        e.printstacktrace();
    }

服务器上的异常是:

12:39:40,990 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/APDTest].[ImageRetrieve]] Servlet.service() for servlet ImageRetrieve threw exception: java.lang.IllegalArgumentException: im == null!
    at javax.imageio.ImageIO.write(ImageIO.java:1457) [:1.6.0_32]
    at javax.imageio.ImageIO.write(ImageIO.java:1521) [:1.6.0_32]
    at com.apd.test.ImageRetrieve.doPost(ImageRetrieve.java:52) [:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.1.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]
    at org.jboss.web.tomcat.service.request.ActiveRequestresponsecacheValve.invoke(ActiveRequestresponsecacheValve.java:53) [:6.1.0.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]

解决方法:

您将图像写入客户端的文件输出流,该流会将内容存储到物理文件中.然后,您尝试将FileOutputStream的toString表示形式转换为图像内容.这是行不通的,该类的toString方法不会为您提供写入流中的内容,它只会显示一条几乎没有帮助的消息,如下所示:

java.io.FileOutputStream@761eec35

我相信您打算做的是使用ByteArrayOutputStream-此类确实允许您检索通过其toString方法编写的实际内容.

在Android方面(摘要):

Bitmap bm = Bitmap.createBitmap(gestureView.getGesture().toBitmap(100,
        100, 8, Color.WHITE));

ByteArrayOutputStream baos = new ByteArrayOutputStream(2056);
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

byte[] imageBytes = baos.toByteArray();
String encodedString = Base64.encodetoString(imageBytes, Base64.DEFAULT);

在服务器端(摘要):

String imageString = request.getParameter("SignatureImage");
byte[] decodedBytes = Base64.decode(imageString, Base64.DEFAULT);

try {
    InputStream in = new ByteArrayInputStream(decodedBytes);
    BufferedImage bImageFromConvert = ImageIO.read(in);

    ImageIO.write(bImageFromConvert, "png", new File(
            "E:/signature/signature.png"));
} catch (IOException e) {
    e.printstacktrace();
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...