【问题标题】:How reduce size of image raw?如何减小图像原始大小?
【发布时间】:2018-07-21 09:53:26
【问题描述】:

我正在使用指纹读取器的 android 上创建一个应用程序,问题是图像是实时的,尺寸为 800x750,字节数组大小为 600000,当将其转换为位图并将其分配时imageview,该应用程序太令人鼓舞和崩溃。 如何减小作为原始图像的字节数组的大小,以便我的应用程序可以毫无问题地绘制指纹

imagePreview.setImageBitmap(imageData.toBitmap());

我试过了,但还是很慢

Bitmap bmp = imageData.toBitmap();
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bmp.compress(Bitmap.CompressFormat.JPEG, 30, stream);
                        byte[] byteArray = stream.toByteArray();

                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                        options.inSampleSize = 2;
                        Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length,options);

【问题讨论】:

    标签: java android bitmap image-resizing


    【解决方案1】:

    你不能。如果您使用的是 ARGB_8888,则图像将占用 4*width*height 字节的内存。没有办法减少它。如果您遇到问题,请查找内存泄漏,并考虑缓存。

    【讨论】:

      【解决方案2】:

      This Answer will help

      如果您希望位图比例相同并减小位图大小。然后通过你的最大值 大小位图。

      public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
          int width = image.getWidth();
          int height = image.getHeight();
      
          float bitmapRatio = (float)width / (float) height;
          if (bitmapRatio > 1) {
              width = maxSize;
              height = (int) (width / bitmapRatio);
          } else {
              height = maxSize;
              width = (int) (height * bitmapRatio);
          }
          return Bitmap.createScaledBitmap(image, width, height, true);
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 2019-04-13
        相关资源
        最近更新 更多