如何从Java字节数组解码图片

时间:2019-05-19 14:27:57

标签: java maven java-ee tomcat7 tomcat8

我从服务器获取一个字节数组,然后将其转换为imageView。问题是,当我使用tomkat maven插件时,一切都很好,但是当我开始使用tomkat版本8时,事实证明

BitmapFactory.decodeByteArray

开始返回null。使用TomCat 8和9(尝试使用7)所有代码后,错误(或更确切地说是null)开始出现

我尝试过7 8 9

     // IMAGE MESSAGE
      //Android client 
          if (message.getImageMessage() != null) {
           MessageViewHolder2 holder = new MessageViewHolder2();
           LayoutInflater messageInflater = (LayoutInflater) 
           context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
           byte [] ar = message.getImageMessage().getByteArray();
           Bitmap bmp = BitmapFactory.decodeByteArray(ar , 0, ar.length);

           if (bmp!=null) {

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 10, out);
            byte[] byteArray = out.toByteArray();

            // MY IMAGE
            if (message.getImageMessage().isFromMe()) {
            // Convert bytes data into a Bitmap
            convertView = 
            messageInflater.inflate(R.layout.image_my_message_layout, null);
            holder.messageBody = (ImageView) 
            convertView.findViewById(R.id.imageMessageView);
            holder.messageBody.setImageBitmap(bmp);

            //Вычисляем ширину и высоту изображения
            double width = bmp.getWidth();
            double height = bmp.getHeight();

            double koef = height / 1000;
            int newWidht = (int) (width / koef);
            int newheight = (int) (height / koef);

            Bitmap bmHalf = Bitmap.createScaledBitmap(bmp, newWidht, newheight, false);
            holder.messageBody.setImageBitmap(bmHalf);
        }
    }

 // Getting pictures on the server
 public  int size =1_000_000;
 int sizeFoto = 0;
 public  ByteBuffer bbuf = ByteBuffer.allocate(size);

 @OnMessage
 public void processUpload(ByteBuffer msg, boolean last, Session session) {

sizeFoto +=msg.array().length;
if(msg.remaining()!=1) {
    bbuf.put(msg);
}
else{
    System.err.println("Size : "+sizeFoto);
    bbuf.put(msg);
    sendBuf(bbuf);
}

}

0 个答案:

没有答案