如何在imageview上灰度图像?

时间:2016-04-13 08:21:39

标签: java android

我是Android的新手,想要创建一个图像处理应用程序。 我已经解决了使用Android手机相机的代码,并在imageview上显示捕获的照片...代码运行良好,问题是我似乎无法使灰度代码工作。或者我似乎无法在imageview上显示灰度图像...请我需要你的帮助。非常感谢你。

这是相机图像捕捉代码,效果很好

public class CameraActivity extends ActionBarActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    Button button = (Button) findViewById(R.id.button);
    //King ina, button2 for processing
    Button button2 = (Button) findViewById(R.id.button2);
    imageView = (ImageView) findViewById(R.id.imageView);

    //Disable the button if it has no camera
    if (!hasCamera())
        button.setEnabled(false);
}

//Check if the user has camera
private boolean hasCamera() {
    return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}

//Launching the camera
public void launchCamera(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    //Take a picture and pass result along to onActivityResult
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

//Show image on imageView
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //Get the photo
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");
        imageView.setImageBitmap(image);
    }
}

}

虽然这是Grayscaling图像的代码...我可以重复显示捕获图像的覆盖代码吗? 非常感谢...

    public Bitmap imageProcess(Bitmap image) {
    int width, height;
    height = image.getHeight();
    width = image.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(image, 0, 0, paint);
    return bmpGrayscale;
    }

3 个答案:

答案 0 :(得分:1)

您可以使用对比度将图像设为黑色和白色。

参见代码..

 public static Bitmap createContrast(Bitmap src, double value) {
// image size 
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap 
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information 
int A, R, G, B;
int pixel;
// get contrast value 
double contrast = Math.pow((100 + value) / 100, 2);

// scan through all pixels 
for(int x = 0; x < width; ++x) {
    for(int y = 0; y < height; ++y) {
        // get pixel color 
        pixel = src.getPixel(x, y);
        A = Color.alpha(pixel);
        // apply filter contrast for every channel R, G, B 
        R = Color.red(pixel);
        R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
        if(R < 0) { R = 0; }
        else if(R > 255) { R = 255; }

        G = Color.red(pixel);
        G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
        if(G < 0) { G = 0; }
        else if(G > 255) { G = 255; }

        B = Color.red(pixel);
        B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
        if(B < 0) { B = 0; }
        else if(B > 255) { B = 255; }

        // set new pixel color to output bitmap 
        bmOut.setPixel(x, y, Color.argb(A, R, G, B));
    } 
} 

return bmOut;
 } 

在mathod调用中将double值设置为50。例如

createContrast(Bitmap src, 50) 

有关公式的更多信息,请参阅this

答案 1 :(得分:1)

经过研究和一些编辑,我用这种方式做了灰度...

  bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

    operation = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    double GS_RED = 0.299;
    double GS_GREEN = 0.587;
    double GS_BLUE = 0.114;
    int pixel;
    int A, R, G, B;
    // get image size
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    // scan through every single pixel
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            // get one pixel color
            pixel = bmp.getPixel(x, y);

            // retrieve color of all channels
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);

            // take conversion up to one single value
            R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
            // set new pixel color to output bitmap
            operation.setPixel(x, y, Color.argb(A, R, G, B));
            //Show grayscaled image
            imageView.setImageBitmap(operation);
        }
    }

感谢您的回答和本网站。 https://xjaphx.wordpress.com/2011/06/21/image-processing-grayscale-image-on-the-fly/

答案 2 :(得分:0)

在图像视图中设置之前,您需要调用imageProcess()方法。

//Show image on imageView
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //Get the photo
        Bundle extras = data.getExtras();
        Bitmap image = (Bitmap) extras.get("data");
        imageView.setImageBitmap(imageProcess(image)); // called here
    }
}