moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

How to change pixels color of TileView in real time #497

Closed Amattia closed 5 years ago

Amattia commented 5 years ago

I need to develop a function that changes the pixel color of an image in real time, I currently wrote this part of the code that seems to work partially

    private void changePixelsColor() {
        try {
            tileView.buildDrawingCache(true);
            tileView.setDrawingCacheEnabled(true);

            int height = tileView.getDrawingCache().getHeight();
            int width = tileView.getDrawingCache().getWidth();
            int[] pixelNumber = new int[height * width];

            Bitmap drawingCache = tileView.getDrawingCache();
            drawingCache.getPixels(pixelNumber, 0, width, 0, 0, width, height);

            Log.d("DEBUG", "Before LOOP");
            for (int i = 0; i < pixelNumber.length; i++) {
                if (String.valueOf(pixelNumber[i]).length() > 2) {
                    String hexColor = "#" + Integer.toHexString(pixelNumber[i]).substring(2);
                    if (hexColor.compareToIgnoreCase("#f0f1f0") == 0) {
                        pixelNumber[i] = Color.RED;
                    }
                }
            }
            Log.d("DEBUG", "After LOOP");
            drawingCache.setPixels(pixelNumber, 0, width, 0, 0, width, height);

            String filename = DAM + activityWizard + "_" + activityIncarico + IMAGE_ANOMALIES;
            File file = new File(PDF_FOLDER_C + filename);
            FileOutputStream out = new FileOutputStream(file);
            Bitmap mappedCar = Bitmap.createBitmap(drawingCache);
            mappedCar.compress(Bitmap.CompressFormat.JPEG, 100, out);
            mappedCar.recycle();
            out.flush();
            out.close();

            tileView.setDrawingCacheEnabled(false);
            tileView.destroyDrawingCache();
        } catch (Exception e) {

        }
    }

I'm using a "TileView" library to show the user an image (I'm simplifying the explanation), in a certain part of the code I need to scan all the pixels of an image, and replace some (eg # f0f1f0 with the red).

Currently I can retrieve the pixel map and understand for each of them what color it is. What I can not do is change the image colors in real time. (The image that i save in a file is it correctly colored)

Is it possible do this ??

moagrius commented 5 years ago

this is not really a library specific question, but i will say that A) i would not use the drawing cache - that means you're going to create a bitmap of the entire tileview instance. you can use the draw method to draw directly onto an appropriately sized bitmap (and maybe even just the one pixel you need), offset by scroll position, etc. beyond that, i know it's possible to read and manipulate pixel data, but you're going to have to override onDraw or onDispatchDraw (or better yet, use a Drawing plugin), and it's really outside the scope of functionality provided by this library.

hth, gl!