简体   繁体   English

RxJava将字节数组转换为位图

[英]RxJava convert byte array to Bitmap

Sorry for my english. 对不起我的英语不好。 Now i learning rxJava 1 and try convert byte[] to Bitmap and set it to imageview. 现在,我学习rxJava 1并尝试将byte []转换为Bitmap并将其设置为imageview。 But it work slowly. 但是它工作缓慢。 Do i right work whith rxJava? 我使用rxJava正确吗? ps: i chek and asynkTask work fater than rxJava, how it possible? ps:我检查和asynkTask的工作比rxJava更好,这怎么可能?

Observable.just(data)
                .map(new Func1<byte[], Bitmap>() {
                    @Override
                    public Bitmap call(byte[] bytes) {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = false;
                        options.inPreferredConfig = Bitmap.Config.RGB_565;
                        options.inDither = true;
                        options.inMutable = true;

                        Bitmap largeBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
                        Bitmap bitmap = Bitmap.createScaledBitmap(largeBitmap
                                , (int) ((float) largeBitmap.getWidth() / 10)
                                , (int) ((float) largeBitmap.getHeight() / 10)
                                , true);

                        if (bitmap.getWidth() > bitmap.getHeight()) {
                            Matrix matrix = new Matrix();
                            matrix.postRotate(90); // anti-clockwise by 90 degrees

                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                        }

                        float k = (float) bitmap.getWidth() / (float) w_target;

                        if (w_target < bitmap.getWidth()) {
                            bitmap = Bitmap.createScaledBitmap(bitmap, w_target, (int) ((float) bitmap.getHeight() / k), true);
                        }

                        return createBlackAndWhite(bitmap);
                    }
                })
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Bitmap>() {
                    @Override
                    public void call(Bitmap bitmap) {
                        imageview.setImageBitmap(bitmap);
                    }
                }); 

Maybe you should change the Scheduler used to handle the process , instead of Schedulers.io() try Schedulers.computation() at least for some of your calcul. 也许您应该更改用于处理该过程的Schedulers.io() ,而不是Schedulers.computation()至少对某些计算尝试使用Schedulers.computation()

According to the documentation 根据文档

computation() : Creates and returns a Scheduler intended for computational work. Calculation() :创建并返回用于计算工作的Scheduler。 This can be used for event-loops, processing callbacks and other computational work. 这可以用于事件循环,处理回调和其他计算工作。 Do not perform IO-bound work on this scheduler. 不要在此调度程序上执行IO绑定工作。 Use Schedulers.io() instead. 请改用Schedulers.io()。

io() : Creates and returns a Scheduler intended for IO-bound work. io() :创建并返回用于IO绑定工作的Scheduler。 The implementation is backed by an Executor thread-pool that will grow as needed. 该实现由Executor线程池支持,该线程池将根据需要增长。 This can be used for asynchronously performing blocking IO. 这可用于异步执行阻塞IO。 Do not perform computational work on this scheduler. 不要在此调度程序上执行计算工作。 Use Schedulers.computation() instead. 请改用Schedulers.computation()。

Hope this helps. 希望这可以帮助。

Sorry for my english. 对不起我的英语不好。

invert the schedulers to 将调度程序反转为

.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())

the thread you observe on is the one in which imageView.setBitmap() will be called, subscribeOn on the other hand will set the thread for the bitmap manipulation 您观察到的线程是其中将调用imageView.setBitmap()的线程,另一方面,subscriptionOn将设置用于位图操作的线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM