转换图像的字节数组,反之亦然 [英] Convert image to byte array and viceversa

查看:193
本文介绍了转换图像的字节数组,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过机器人发送图像的字节数组,并将其转换回服务器端png文件,我得到抛出:IllegalArgumentException 在服务器端。

这是我的Andr​​oid code的转换图像字节数组,将其作为请求给服务器:

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

            位图BM = Bitmap.createBitmap(gestureView.getGesture()toBitmap(100,100,第8,Color.WHITE)。);

            FileOutputStream中FOS = openFileOutput(signature.png,Context.MODE_PRIVATE);
            bm.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);

            byte []的image_byte = fos.toString()的GetBytes(); // Base64.en code(fos.toString()的GetBytes(),0);

            串strImage = image_byte.toString();
 

这是我的服务器端程序:

 字符串和imagestring =的request.getParameter(SignatureImage);
    byte []的imageByte = imageString.getBytes();
    尝试 {
        InputStream的时间=新ByteArrayInputStream的(imageByte);
        BufferedImage的bImageFromConvert = ImageIO.read(中);

        //这行获得异常
        ImageIO.write(bImageFromConvert,PNG,新的文件(
                E:/signature/signature.png));
        / * BufferedImage中的BufferedImage = ImageIO.read(新ByteArrayInputStream的(imageByte));
        ImageIO.write(BufferedImage的,JPG,新的文件(E:/signature/signature.jpg)); * /
    }赶上(IOException异常E){
        e.printStackTrace();
    }
 

异常的服务器是:

  12:39:40990错误[。org.apache.catalina.core.ContainerBase [jboss.web] [本地主机] [/ APDTest] [ImageRetrieve] Servlet的。 service()方法的servlet的ImageRetrieve抛出异常:java.lang.IllegalArgumentException:如果IM == NULL!
    在javax.imageio.ImageIO.write(ImageIO.java:1457):1.6.0_32]
    在javax.imageio.ImageIO.write(ImageIO.java:1521):1.6.0_32]
    在com.apd.test.ImageRetrieve.doPost(ImageRetrieve.java:52)[:]
    在javax.servlet.http.HttpServlet.service(HttpServlet.java:754):1.0.0.Final]
    在javax.servlet.http.HttpServlet.service(HttpServlet.java:847):1.0.0.Final]
    在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324):6.1.0.Final]
    在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242):6.1.0.Final]
    在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275):6.​​1.0.Final]
    在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161):6.1.0.Final]
    在org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181):6.1.0.Final]
    在org.jboss.modcluster.catalina.CatalinaContext $ RequestListenerValve.event(CatalinaContext.java:285):1.1.0.Final]
    在org.jboss.modcluster.catalina.CatalinaContext $ RequestListenerValve.invoke(CatalinaContext.java:261):1.1.0.Final]
    在org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88):6.1.0.Final]
    在org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
    在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159):6.1.0.Final]
    在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102):6.1.0.Final]
    在org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158):6.1.0.Final]
    在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109):6.1.0.Final]
    在org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]
    在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362):6.1.0.Final]
    在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877):6.1.0.Final]
    在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:654):6.1.0.Final]
    在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:951):6.1.0.Final]
 

解决方案

您写的形象在你的客户端的文件输出流,将内容存储到一个物理文件。然后,您试图将的toString 转换重新的FileOutputStream presentation的图像内容。这是行不通的,这个类的的toString 方法不给你写到流中的内容,它只是打印这样一个勉强有用的信息:

  

java.io.FileOutputStream@761eec35

我相信你打算做什么用 ByteArrayOutputStream - 这个类的确实允许您检索通过其的toString 的方法写的实际内容。

在Android上侧(片段):

 位图BM = Bitmap.createBitmap(gestureView.getGesture()。toBitmap(100,
        100,如图8所示,Color.WHITE));

ByteArrayOutputStream BAOS =新ByteArrayOutputStream(2056);
bm.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);

byte []的imageBytes = baos.toByteArray();
字符串连接codedString = Base64.en codeToString(imageBytes,Base64.DEFAULT);
 

在服务器端(片段):

 字符串和imagestring =的request.getParameter(SignatureImage);
byte []的德codedBytes = Base64.de code(和imagestring,Base64.DEFAULT);

尝试 {
    InputStream的时间=新ByteArrayInputStream的(去codedBytes);
    BufferedImage的bImageFromConvert = ImageIO.read(中);

    ImageIO.write(bImageFromConvert,PNG,新的文件(
            E:/signature/signature.png));
}赶上(IOException异常E){
    e.printStackTrace();
}
 

I'm trying to send an image as byte array through android, and convert it back to png file on server side, I'm getting IllegalArgumentException on server side.

This is my android code which converts image to byte array and sends it as request to server:

 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();

This is my server side program:

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();
    }

Exception on server is:

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]

解决方案

Your writing the image to a file output stream on your client side, which will store the contents into a physical file. You are then trying to convert the toString representation of FileOutputStream as the image content. This won't work, the toString method of that class doesn't give you the contents that were written to the stream, it just prints a barely helpful message like this:

java.io.FileOutputStream@761eec35

I believe what you intended to do was use a ByteArrayOutputStream - this class does allow you to retrieve the actual contents written via its toString method.

On Android side (snippet):

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);

On server side (snippet):

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();
}

这篇关于转换图像的字节数组,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆