Как сделать Pixel Clearing только для цвета изображения «смежным» - PullRequest
0 голосов
/ 09 июля 2019

В моем приложении нужно очистить изображения, удалить фоновый цвет и сохранить изображение в формате PNG, поэтому я использую удалить любой цвет, который я коснусь, но мне нужно, чтобы он работал с тем же непрерывным цветом

Я использую это, но это очищает цвет в изображении смежным и непрерывным, мне нужно только для работы с смежным цветом

private static class AutoPixelClearingTask extends AsyncTask<Integer, Void, Bitmap> {

        private WeakReference<DrawView> drawViewWeakReference;

        AutoPixelClearingTask(DrawView drawView) {
            this.drawViewWeakReference = new WeakReference<>(drawView);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            drawViewWeakReference.get().loadingModal.setVisibility(VISIBLE);
            drawViewWeakReference.get().cuts.push(new Pair<>(null, drawViewWeakReference.get().imBitmap));
        }

        @Override
        protected Bitmap doInBackground(Integer... points) {
            Bitmap oldBitmap = drawViewWeakReference.get().imBitmap;

            int colorToReplace = oldBitmap.getPixel(points[0], points[1]);

            int width = oldBitmap.getWidth();
            int height = oldBitmap.getHeight();
            int[] pixels = new int[width * height];
            oldBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

            int rA = Color.alpha(colorToReplace);
            int rR = Color.red(colorToReplace);
            int rG = Color.green(colorToReplace);
            int rB = Color.blue(colorToReplace);

            int pixel;

            // iteration through pixels
            for (int y = 0; y < height; ++y) {
                for (int x = 0; x < width; ++x) {
                    // get current index in 2D-matrix
                    int index = y * width + x;
                    pixel = pixels[index];
                    int rrA = Color.alpha(pixel);
                    int rrR = Color.red(pixel);
                    int rrG = Color.green(pixel);
                    int rrB = Color.blue(pixel);

                    if (rA - COLOR_TOLERANCE < rrA && rrA < rA + COLOR_TOLERANCE && rR - COLOR_TOLERANCE < rrR && rrR < rR + COLOR_TOLERANCE &&
                            rG - COLOR_TOLERANCE < rrG && rrG < rG + COLOR_TOLERANCE && rB - COLOR_TOLERANCE < rrB && rrB < rB + COLOR_TOLERANCE) {
                        pixels[index] = Color.TRANSPARENT;
                    }
                }
            }

            Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);

            return newBitmap;
        }

        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            drawViewWeakReference.get().imBitmap = result;
            drawViewWeakReference.get().undoButton.setEnabled(true);
            drawViewWeakReference.get().loadingModal.setVisibility(INVISIBLE);
            drawViewWeakReference.get().invalidate();
        }
    }

Исходное изображение enter image description here

очищенное изображение enter image description here

что я хочу enter image description here

...