加载/显示位图的最快方法;重用Bitmap的最佳方法

时间:2013-06-20 09:48:42

标签: android image-processing bitmap

我需要以每秒不同的帧数显示图像,最大FPS为30.图像来自SD卡,并且都具有相同的尺寸:480 x 640。 我创建了3个可能的解决方案,但每个解决方案都存在问题:

以下结果为30 FPS。

予。不重复使用位图

  • 很多GC电话:aprox。 每秒30 GC
  • CPU负载:达到 92%

    private Bitmap bitmap;
    
    private void startAnimation1() {
        TimerTask updateImage = new UpdateImage1();
        timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS);
    }
    
    class UpdateImage1 extends TimerTask {
        @Override
        public void run() {
            try {
                if (i == IMAGES_NR) {
                    i = 0;
                }
                bitmap = BitmapFactory.decodeStream(new FileInputStream(framesFiles[i]), null, null);
                i++;
            } catch (FileNotFoundException e) {
                System.out.println("Exception 1: " + e.getMessage());
            }
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imgView.setImageBitmap(bitmap);
                }
            });
        }
    }
    

II。通过BitmapFactory.Options.inBitmap

重用位图
  • GC通话较低 - 每秒1或2
  • CPU负载:达到 84%

运行动画后,应用程序崩溃了:

06-20 15:08:58.158: WARN/System.err(7880): java.lang.ArrayIndexOutOfBoundsException: length=-5131855; regionStart=0; regionLength=1024
06-20 15:08:58.158: WARN/System.err(7880): at java.util.Arrays.checkOffsetAndCount(Arrays.java:1731)
06-20 15:08:58.158: WARN/System.err(7880): at java.io.BufferedInputStream.read(BufferedInputStream.java:273)
06-20 15:08:58.158: WARN/System.err(7880): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-20 15:08:58.158: WARN/System.err(7880): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
06-20 15:08:58.158: WARN/System.err(7880): at com.example.SendPreviewOptimization.MyActivity$UpdateImage2.run(MyActivity.java:148)
06-20 15:08:58.158: WARN/System.err(7880): at java.util.Timer$TimerImpl.run(Timer.java:284)
06-20 15:08:58.168: DEBUG/skia(7880): ---- read threw an exception
06-20 15:08:58.168: DEBUG/skia(7880): --- decoder->decode returned false
06-20 15:08:58.168: WARN/System.err(7880): java.lang.IllegalArgumentException: Problem decoding into existing bitmap
06-20 15:08:58.168: WARN/System.err(7880): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:590)
06-20 15:08:58.168: WARN/System.err(7880): at com.example.SendPreviewOptimization.MyActivity$UpdateImage2.run(MyActivity.java:148)
06-20 15:08:58.168: WARN/System.err(7880): at java.util.Timer$TimerImpl.run(Timer.java:284)
06-20 15:08:58.178: ERROR/msm8960.hwcomposer(330): prepareBypass: Unable to setup bypass due to non-pmem memory
06-20 15:08:58.198: ASSERT/libc(7880): Fatal signal 11 (SIGSEGV) at 0xffd1d447 (code=1)
06-20 15:08:58.238: ERROR/msm8960.hwcomposer(330): prepareBypass: Unable to setup bypass due to non-pmem memory
06-20 15:08:58.498: ERROR/MP-Decision(1448): DOWN Ld:25 Ns:1.100000 Ts:190 rq:0.000000 seq:194.000000
06-20 15:08:58.708: INFO/DEBUG(27660): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***



    private static BitmapFactory.Options bitmapOptions;
    private FileInputStream in;

    private void startAnimation2() {
        bitmapOptions = new BitmapFactory.Options();
        // setup bitmap reuse options:
        bitmapOptions.inPurgeable = true;
        bitmapOptions.inInputShareable = true;
        bitmapOptions.inBitmap = reusableBitmap;
        bitmapOptions.inMutable = true;
        bitmapOptions.inSampleSize = 1;

        TimerTask updateImage = new UpdateImage2();
        timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS);
    }

    class UpdateImage2 extends TimerTask {
        @Override
        public void run() {
            try {
                if (i == IMAGES_NR) {
                    i = 0;
                }

                //** version 1:
                in = new FileInputStream(framesFiles[i]);
                //decode into existing bitmap
                BitmapFactory.decodeStream(in, null, bitmapOptions);
                in.close();

                //** version 2:
                //BitmapFactory.decodeFile(framesFiles[i].getAbsolutePath(), bitmapOptions);

                i++;
            } catch (Exception e) {
                System.out.println("Exception 2: " + e.getMessage());
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imgView.setImageBitmap(reusableBitmap);
                }
            });
        }
    }

III。选项III:使用缓冲区(使Bytebuffer更高效的一件事是使用直接内存。)

  • 这个选项让我无法工作:(

    private ByteBuffer buffer;
    private byte[] b;
    private IntBuffer mPixels;
    
    private void startAnimation3() {
        buffer = ByteBuffer.allocate(480 * 640 * 6);
        b = new byte[480 * 640 * 6];
        TimerTask updateImage = new UpdateImage3();
        timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS);
    }
    
    class UpdateImage3 extends TimerTask {
        public void run() {
            try {
                if (i == IMAGES_NR) {
                    i = 0;
                }
                FileInputStream frameInputStream = new FileInputStream(framesFiles[i]);
                frameInputStream.read(b);
                buffer.wrap(b);
                buffer.position(0);
                reusableBitmap.copyPixelsFromBuffer(buffer);
                frameInputStream.close();
                i++;
            } catch (Exception e) {
                System.out.println("Exception 3: " + e.getMessage());
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imgView.setImageBitmap(reusableBitmap);
                }
            });
        }
    }
    
    private ByteBuffer copyToBuffer(Bitmap bitmap) {
        int size = bitmap.getHeight() * bitmap.getRowBytes();
        ByteBuffer buffer = ByteBuffer.allocateDirect(size);
        bitmap.copyPixelsToBuffer(buffer);
        return buffer;
    }
    

在上述每个解决方案中,我都会收到logcat的

ERROR/msm8960.hwcomposer(330): prepareBypass: Unable to setup bypass due to non-pmem memory

我不知道究竟意味着什么。

我以前没有使用Bitmap重用,也不知道哪个是最好的解决方案。

我添加了我在这里创建的项目:https://www.dropbox.com/sh/3xov369u1bmjpd1/qBQax4t48D以及2帧/图像。

回答Neron T

我试过那个图书馆:

    //Option IV:
    private AQuery aquery;

    private void startAnimation4() {
        aquery = new AQuery(this);
        aquery.id(R.id.imgView);

        TimerTask updateImage = new UpdateImage4();
        timer.scheduleAtFixedRate(updateImage, 0, 1000 / FPS);
    }

    class UpdateImage4 extends TimerTask {
        public void run() {
            try {
                if (i == 29) {
                    i = 0;
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //load image from file, down sample to target width of 300 pixels
                        aquery.image(framesFiles[i],300);
                    }
                });
                i++;
            } catch (Exception e) {
                System.out.println("Exception 4: " + e.getMessage());
            }
        }
    }

它不能像我预期的那样工作 - 我在每张照片之前都有一个闪烁的效果。我认为首先它会清除图片,然后添加一个新图片:(

1 个答案:

答案 0 :(得分:0)

请注意,您无法修改内部线程中不是UIThread(主要部分)的视图,请尝试使用AsyncTasks。使用图像而不必过多思考的简单方法是使用Android Query这样的框架,看看here

如果您想手动检查thisthis