【问题标题】:Color Replacer in AndroidAndroid中的颜色替换器
【发布时间】:2013-07-03 10:20:32
【问题描述】:

我想替换图像中的颜色并将其分配给 imageview 我在 google 中搜索了很多时间,但仍然没有找到任何有用的资源。我在 java rgbimagefilter 中看到过,但是它没有在 android 中使用所以我在屏幕截图下方的例外输出:

原图

将绿色替换为灰色后,如下图所示:

我知道基本的想法,比如读取图像每个像素比较 rgb 值,因为它的匹配替换为新颜色,但我不知道如何在 android 中以编程方式做到这一点。

【问题讨论】:

标签: android image-processing colorfilter


【解决方案1】:

这里有一些建议(下次尝试搜索图像处理;-)):

Aviary SDK -> And the code for it.

Here你可以找到一个很好的各种图像处理教程。

在这里你可以找到一些库:

最后是这个项目here

阅读愉快:-)

【讨论】:

    【解决方案2】:

    如果您不想使用任何第三方库,可以查看以下代码以帮助您入门:

    package pete.android.study;
    
    import android.graphics.Bitmap;
    
    public class ImageProcessor {
        Bitmap mImage;
        boolean mIsError = false;
    
    public ImageProcessor(final Bitmap image) {
        mImage = image.copy(image.getConfig(), image.isMutable());
        if(mImage == null) {
            mIsError = true;
        }
    }
    
    public boolean isError() {
        return mIsError;
    }
    
    public void setImage(final Bitmap image) {
        mImage = image.copy(image.getConfig(), image.isMutable());
        if(mImage == null) {
            mIsError = true;
        } else {
            mIsError = false;
        }
    }
    
    public Bitmap getImage() {
        if(mImage == null){
            return null;
        }
        return mImage.copy(mImage.getConfig(), mImage.isMutable());
    }
    
    public void free() {
        if(mImage != null && !mImage.isRecycled()) {
            mImage.recycle();
            mImage = null;
        }
    }
    
    public Bitmap replaceColor(int fromColor, int targetColor) {
        if(mImage == null) {
            return null;
        }
    
        int width = mImage.getWidth();
        int height = mImage.getHeight();
        int[] pixels = new int[width * height];
        mImage.getPixels(pixels, 0, width, 0, 0, width, height);
    
        for(int x = 0; x < pixels.length; ++x) {
            pixels[x] = (pixels[x] == fromColor) ? targetColor : pixels[x];
        }
    
        Bitmap newImage = Bitmap.createBitmap(width, height, mImage.getConfig());
        newImage.setPixels(pixels, 0, width, 0, 0, width, height);
    
        return newImage;
        }
    }
    

    此代码不是我的,是从另一个 SO 用户的 answer 在此 site 上找到的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多