2016-04-13 26 views
2

Android için çok yeni ve bir görüntü işleme uygulaması oluşturmak istiyorum. Bir android telefonun kamerasını kullanarak bir kodu kaldırdım ve yakalanan fotoğrafı bir resim görünümünde gösteriyor ... Kod iyi çalışıyor, sorun şu ki gri tonlama kodunun çalışmasını sağlayamıyorum. Ya da görüntü görüntüsünde gri tonlamalı resmi gösteremiyorum ... Lütfen yardımınıza ihtiyacım var. Çok teşekkür ederim.Görüntü görüntüsünde bir görüntü nasıl gri tonlamalıdır?

Bu kamera görüntüsü Bu resmi grayscaling kodudur iken de

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); 
    } 
} 

} 

çalışır kod kaydı yapıyor ... ben sadece yakalanan görüntüyü gösterir iptal kodunu tekrarlar mısınız? Çok teşekkürler ... Sen görüntü görünümünde ayarlamadan önce, sizin imageProcess() yöntemini çağırmanız gerekir

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; 
    } 

cevap

1

Merhaba, görüntüyü kontrast kullanarak siyah beyaz yapabilirsiniz.

Mathod çağrısı 50 çift değeri ayarlayın

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; 
} 

.. koduna bakınız. Örnek

createContrast(Bitmap src, 50) 

için formüller hakkında daha fazla bilgi için araştırma ve birkaç düzenlemeden sonra this

+0

Yanıtı kabul ettiğiniz için teşekkür ederiz. –

0

.

//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 
    } 
} 
+1

Bunu deneyeceğim, çok teşekkürler efendim! –

+0

Çok doğru! Android'de çok noob var ... Ama araştırma yaptıktan sonra, iyi bir gri tonlama kodu buldum. –

+0

@JericoManalastas Bu yardım mı? Lütfen doğru cevabı işaretleyin. Ve size yardımcı olan cevapları kaçırmayın. Teşekkürler. – Doomsknight

1

bakınız, ben grayscaling bu şekilde yaptım ... cevaplar ve bunun için

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); 
     } 
    } 

Teşekkür bir site. https://xjaphx.wordpress.com/2011/06/21/image-processing-grayscale-image-on-the-fly/

+0

İyi şeyler. Bir çözüm bulmana sevindim. Doğru olarak işaretle. :) – Doomsknight

İlgili konular