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. But it work slowly. Do i right work whith rxJava? ps: i chek and asynkTask work fater than rxJava, how it possible?

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);
                    }
                }); 
lelloman

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related