将字节数组转换为图像

时间:2018-04-06 14:20:21

标签: java arrays image

我正在开发一个实用工具,它从ActiveMQ获取JPEG字节消息并将其转换为字节数组,并将其发送到字节数组中的其他应用程序。

我想检查特定字节消息是否格式正确并且可以转换为图像格式。当我尝试将其转换为图像格式时,它总是给我空指针异常。任何人都可以帮忙吗?

代码如下:

if (message instanceof BytesMessage) {
    BUSINESS_LOGGER.info("bytes message Recieved");
    BytesMessage bMessage = (BytesMessage) message;
    //BufferedImage imagebyte= null;
    byte[] imageinbytearray;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int n;
    byte[] bytemesage = new byte[message.toString().length()];

    while ((n = bMessage.readInt()) !=- 1) {
        out.write(bytemesage, 0, n);
    }

    imageinbytearray= out.toByteArray();
    itemDamage.setPicture(imageinbytearray);

    // itemDamage is a soap operation.
    try {
        BufferedImage imagebyte = ImageIO.read(new ByteArrayInputStream(imageinbytearray));
        File f = new File("c:/Image.jpeg");
        ImageIO.write(imagebyte, "JPEG", f);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
} else {
    BUSINESS_LOGGER.info("No message format Recieved");
}

imagebyte始终为null

java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
    at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
    at javax.imageio.ImageIO.write(ImageIO.java:1520)
    at se.posten.loab.lisp.csp.core.serviceimpl.NovaItemDamageServiceBean.createItemDamage(NovaItemDamageServiceBean.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.delegateInterception(Jsr299BindingsInterceptor.java:74)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:84)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:97)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)
    at org.sculptor.framework.persistence.JpaFlushEagerInterceptor.invoke(JpaFlushEagerInterceptor.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:89)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)
    at org.sculptor.framework.errorhandling.ErrorHandlingInterceptor.invoke(ErrorHandlingInterceptor.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:89)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:73)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:258)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:347)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:243)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:185)
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:185)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:73)
    at se.posten.loab.lisp.csp.core.serviceapi.NovaItemDamageServiceLocal$$$view148.createItemDamage(Unknown Source)    
    at se.posten.loab.lisp.csp.core.consumer.NovaDamageItemConsumerBean.handleMessage(NovaDamageItemConsumerBean.java:63)    
    at se.posten.loab.lisp.csp.core.consumer.NovaDamageItemConsumerBean.onMessage(NovaDamageItemConsumerBean.java:55)   
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    
    at java.lang.reflect.Method.invoke(Method.java:606)   
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)    
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)   
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:86)    
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:97)    
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)    
    at org.sculptor.framework.persistence.JpaFlushEagerInterceptor.invoke(JpaFlushEagerInterceptor.java:41)    
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   
    at java.lang.reflect.Method.invoke(Method.java:606)   
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:89)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)    
    at org.sculptor.framework.errorhandling.ErrorHandlingInterceptor.invoke(ErrorHandlingInterceptor.java:55)    
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   
    at java.lang.reflect.Method.invoke(Method.java:606)  
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:89)   
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)   
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:374)    
    at org.sculptor.framework.context.ServiceContextStoreInterceptor.invoke(ServiceContextStoreInterceptor.java:50)    
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)   
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    
    at java.lang.reflect.Method.invoke(Method.java:606)   
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptor.java:89)   
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)   
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)   
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)   
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)   
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:93)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)    
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)   
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:258)    
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:347)    
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:243)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ejb3.component.messagedriven.MessageDrivenComponentDescription$5$1.processInvocation(MessageDrivenComponentDescription.java:203)   
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)   
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:185)    
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:185)    
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)    
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)    
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:73)    
    at javax.jms.MessageListener$$$view102.onMessage(Unknown Source)    
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    
    at java.lang.reflect.Method.invoke(Method.java:606)    
    at org.jboss.as.ejb3.inflow.MessageEndpointInvocationHandler.doInvoke(MessageEndpointInvocationHandler.java:140)    
    at org.jboss.as.ejb3.inflow.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:73)    
    at com.sun.proxy.$Proxy163.onMessage(Unknown Source)    
    at org.apache.activemq.ra.MessageEndpointProxy$MessageEndpointAlive.onMessage(MessageEndpointProxy.java:123)    
    at org.apache.activemq.ra.MessageEndpointProxy.onMessage(MessageEndpointProxy.java:64)    
    at org.apache.activemq.ActiveMQSession.run(ActiveMQSession.java:916)    
    at org.apache.activemq.ra.ServerSessionImpl.run(ServerSessionImpl.java:169)    
    at org.jboss.jca.core.workmanager.WorkWrapper.run(WorkWrapper.java:218)    
    at org.jboss.threads.SimpleDirectExecutor.execute(SimpleDirectExecutor.java:33)    
    at org.jboss.threads.QueueExecutor.runTask(QueueExecutor.java:808)    
    at org.jboss.threads.QueueExecutor.access$100(QueueExecutor.java:45)    
    at org.jboss.threads.QueueExecutor$Worker.run(QueueExecutor.java:828)    
    at java.lang.Thread.run(Thread.java:745)    
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)

这是堆栈跟踪。问题不在于空指针,但问题是BufferedImage imagebyte = ImageIO.read(new ByteArrayInputStream(imageinbytearray));即使imageinbytearray有字节数组,该行也会返回空值

0 个答案:

没有答案