Advertisement
Kaidul

BrightAndContrast

Jan 4th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package com.kaidul.contrastandbrightness;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Canvas;
  8. import android.graphics.ColorMatrix;
  9. import android.graphics.ColorMatrixColorFilter;
  10. import android.graphics.Matrix;
  11. import android.graphics.Paint;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.Display;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.Button;
  19. import android.widget.ImageView;
  20.  
  21. public class MainActivity extends Activity implements OnClickListener {
  22.   ImageView chosenImageView;
  23.   Button choosePicture;
  24.   @Override
  25.   public void onCreate(Bundle savedInstanceState) {
  26.     super.onCreate(savedInstanceState);
  27.     setContentView(R.layout.activity_main);
  28.  
  29.     chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
  30.     choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
  31.  
  32.     choosePicture.setOnClickListener(this);
  33.   }
  34.  
  35.   public void onClick(View v) {
  36.     Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
  37.         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  38.     startActivityForResult(choosePictureIntent, 0);
  39.   }
  40.  
  41.   protected void onActivityResult(int requestCode, int resultCode,
  42.       Intent intent) {
  43.     super.onActivityResult(requestCode, resultCode, intent);
  44.  
  45.     if (resultCode == RESULT_OK) {
  46.       Uri imageFileUri = intent.getData();
  47.  
  48.       Display currentDisplay = getWindowManager().getDefaultDisplay();
  49.       int dw = currentDisplay.getWidth();
  50.       int dh = currentDisplay.getHeight() / 2 - 100;
  51.  
  52.       try {
  53.        
  54.         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
  55.         bmpFactoryOptions.inJustDecodeBounds = true;
  56.         Bitmap bmp = BitmapFactory
  57.             .decodeStream(getContentResolver().openInputStream(
  58.                 imageFileUri), null, bmpFactoryOptions);
  59.  
  60.         int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
  61.             / (float) dh);
  62.         int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
  63.             / (float) dw);
  64.  
  65.         if (heightRatio > 1 && widthRatio > 1) {
  66.           if (heightRatio > widthRatio) {
  67.             bmpFactoryOptions.inSampleSize = heightRatio;
  68.           } else {
  69.             bmpFactoryOptions.inSampleSize = widthRatio;
  70.           }
  71.         }
  72.         bmpFactoryOptions.inJustDecodeBounds = false;
  73.         bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
  74.         Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
  75.             .getHeight(), bmp.getConfig());
  76.         Canvas canvas = new Canvas(alteredBitmap);
  77.         Paint paint = new Paint();
  78.         ColorMatrix cm = new ColorMatrix();
  79.         // Increase Contrast, Slightly Reduce Brightness
  80.         float contrast = 3;
  81.         float brightness = 3;
  82.         cm.set(new float[] { contrast, 0, 0, 0, brightness, 0,
  83.             contrast, 0, 0, brightness, 0, 0, contrast, 0,
  84.             brightness, 0, 0, 0, 1, 0 });
  85.        
  86.         paint.setColorFilter(new ColorMatrixColorFilter(cm));
  87.         Matrix matrix = new Matrix();
  88.         canvas.drawBitmap(bmp, matrix, paint);
  89.         ImageView alteredImageView = (ImageView) this
  90.             .findViewById(R.id.AlteredImageView);
  91.         alteredImageView.setImageBitmap(alteredBitmap);
  92.         chosenImageView.setImageBitmap(bmp);
  93.       } catch (Exception e) {
  94.         Log.v("ERROR", e.toString());
  95.       }
  96.     }
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement