[英]How to grayscale an image on imageview?
我對 Android 很陌生,想創建一個圖像處理應用程序。 我已經解除了使用 android 手機相機的代碼,並在 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);
}
}
}
雖然這是對圖像進行灰度化的代碼...我可以重復顯示捕獲圖像的覆蓋代碼嗎? 非常感謝...
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;
}
嗨,您可以使用對比度使圖像變成黑色和白色。
看代碼..
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)
有關公式的更多信息,請參閱此
經過研究和一些編輯后,我以這種方式進行了灰度化...
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/
在圖像視圖中設置它之前,您需要調用您的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
}
}
使用此依賴項進行圖像轉換
Github 鏈接 - https://github.com/wasabeef/glide-transformations
repositories { mavenCentral()}
implementation 'jp.wasabeef:glide-transformations:4.3.0'
Glide.with(this).load(R.drawable.demo).apply(RequestOptions.bitmapTransform(GrayscaleTransformation())).into(imageView)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.