android位图颜色转换-非常慢

wfsdck30  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(194)

我正在做一个关于android(java)中图像转换的小型自学项目。出于某种原因,在手机lg k10(2017)上加载应用程序时,图像转换运行非常缓慢,与内置应用程序相比,可能需要一分钟或更长时间-类似的转换最多需要几秒钟。这些文件指的是大约4MB大的文件。在100KB这样的小图片上试用时,两种方法都需要几秒钟。
我试着在代码中添加一些时间戳,似乎是双循环造成了延迟,但是没有其他方法可以访问所有像素(?)。
有什么我不知道的吗?
活动代码:

public void checkButton(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        BlackAndWhite bw = new BlackAndWhite(initialBitMap);
        switch (view.getId()) {
            case R.id.radioButtonRMS:
                if (checked) {

                    afterConversionBitmap =    bw.getBlackAndWhiteImage(BlackAndWhite.Method.rootMeanSquare);

                }
                break;
            case R.id.radioButtonLUMINOSITY:
                if (checked) {

                    afterConversionBitmap = bw.getBlackAndWhiteImage(BlackAndWhite.Method.luminosity);
                }

                break;

            case R.id.radioButtonAVERAGE:
                if (checked) {

                    afterConversionBitmap = bw.getBlackAndWhiteImage(BlackAndWhite.Method.average);

                }

                break;
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageView.setImageBitmap(afterConversionBitmap);
            }
        });

黑白班

import android.graphics.Bitmap;
import android.graphics.Color;

public class BlackAndWhite {

    private Bitmap imageIn;
    private Bitmap imageOut;
    private int red;
    private int green;
    private int blue;
    private int rgb;
    private int tempColor;

    /**
     * Class converts given image into black and white spectrum. This constructor loads image into memory. Conversion happens during recalling get method
     *
     * @param imageIn BufferedImage to be loaded
     */
    public BlackAndWhite(Bitmap imageIn) {
        this.imageIn = imageIn;
    }

    /**
     * Method of color conversion.
     */
    public enum Method {
        rootMeanSquare, average, luminosity;
    }

    /**
     * Getting conversed image into black and white spectrum.
     *
     * @param method type of color conversion (BlackAndWhite.Method enum).
     * @return image after black and white conversion. Bitmap type.
     */

    public Bitmap getBlackAndWhiteImage(Method method) {

        imageOut = Bitmap.createBitmap(imageIn.getWidth(), imageIn.getHeight(), Bitmap.Config.ARGB_8888);

        for (int yPosition = 0; yPosition < imageIn.getHeight(); yPosition++) {
            for (int xPosition = 0; xPosition < imageIn.getWidth(); xPosition++) {
                tempColor = (imageIn.getPixel(xPosition, yPosition));
                if (Color.alpha(tempColor) == 0) {

                    continue;
                } else {

                    red = Color.red(tempColor);
                    green = Color.green(tempColor);
                    blue = Color.blue(tempColor);

                    if (method == Method.luminosity) {

                        rgb = (int) (red * 0.2126 + green * 0.7152 + blue * 0.0722);

                    } else if (method == Method.average) {

                        rgb = (red + green + blue) / 3;

                    } else if (method == Method.rootMeanSquare) {

                        rgb = (int) (Math.sqrt((Math.pow(red, 2) + Math.pow(green, 2) + Math.pow(blue, 2))));

                        if (rgb > 255) {
                            rgb = 255;
                        }

                    }

                    imageOut.setPixel(xPosition, yPosition, Color.rgb(rgb, rgb, rgb));

                }

            }
        }

        return imageOut;
    }

}

p、 这只是一个实验,我知道以后,我应该做一个后台线程,一个处理程序等。
彼得

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题