2016-04-13 7 views
2

Ich bin sehr neu in Android und möchte eine Bildverarbeitungs-App erstellen. Ich habe einen Code in die Kamera eines Android-Handys aufgenommen und zeigt das aufgenommene Foto auf einer Bildansicht ... Der Code funktioniert gut, das Problem ist, dass ich nicht scheinen kann, um den Grayscaling-Code zu arbeiten. Oder ich kann nicht scheinen, das Graustufenbild auf der Bildansicht zu zeigen ... Bitte brauche ich deine Hilfe. Vielen Dank.Wie skaliert man ein Bild in der Bildansicht?

Erfassung Kamera Code Dies ist das Bild, das

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

} 

Während dies ist der Code für Grauskalierung des Bildes gut funktioniert ... Kann ich den Aufhebungscode nur wiederholen, die das aufgenommene Bild zeigt? Vielen Dank ...

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

Antwort

1

Hallo können Sie das Bild schwarz n weiß mit Kontrast machen.

Siehe den Code ..

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

den doppelten Wert auf 50 auf mathod Anruf ein. Für das Beispiel

createContrast(Bitmap src, 50) 

Weitere Informationen zu Formeln finden Sie this

+0

Vielen Dank für die Annahme der Antwort –

0

Sie benötigen imageProcess() Methode aufrufen, bevor es in der Bildansicht Einstellung.

//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

Ich werde das versuchen, vielen Dank Herr! –

+0

Also wahr! Ich bin so noob in Android ... Aber nach der Recherche und allen habe ich einen guten Grauton Code gefunden. –

+0

@JericoManalastas Hat diese Hilfe geholfen? Bitte markieren Sie die richtige Antwort. Und upvote alle Antworten, die Ihnen geholfen haben. Vielen Dank. – Doomsknight

1

Nach der Untersuchung und ein paar Bearbeitung, habe ich die Grauskalierung auf diese Weise ...

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

Danke für die Antworten und diese Seite? ˅. https://xjaphx.wordpress.com/2011/06/21/image-processing-grayscale-image-on-the-fly/

+0

Gute Sachen. Schön, dass Sie eine Lösung gefunden haben. Markieren Sie es als richtig. :) – Doomsknight

Verwandte Themen