代码之家  ›  专栏  ›  技术社区  ›  Mattia

如何实时更改图像的像素颜色

  •  0
  • Mattia  · 技术社区  · 7 年前

    我需要开发一个函数来实时更改图像的像素颜色,我目前编写的这部分代码似乎部分有效

    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];
    
            tileView.getDrawingCache().getPixels(pixelNumber, 0, width, 0, 0, width, height);
    
            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;
                    }
                }
            }
    
            tileView.getDrawingCache().setPixels(pixelNumber, 0, width, 0, 0, width, height);
    
            File file = new File(MYPATH);
            FileOutputStream out = new FileOutputStream(file);
            Bitmap mappedCar = Bitmap.createBitmap(tileView.getDrawingCache());
            mappedCar.compress(Bitmap.CompressFormat.JPEG, 100, out);
            mappedCar.recycle();
            out.flush();
            out.close();
    
            tileView.setDrawingCacheEnabled(false);
            tileView.destroyDrawingCache();
        } catch (Exception e) {
    
        }       
    }
    

    具体地说,我使用“TileView”库向用户显示一幅图像(我简化了解释),在代码的某个部分,我需要扫描图像的所有像素,并替换一些像素(例如,用红色替换#f0f1f0)。

    /*********************编辑1*********************/

    enter image description here

    0 回复  |  直到 7 年前