Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kaidul.contrastandbrightness;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorMatrix;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Display;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends Activity implements OnClickListener {
- ImageView chosenImageView;
- Button choosePicture;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
- choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
- choosePicture.setOnClickListener(this);
- }
- public void onClick(View v) {
- Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(choosePictureIntent, 0);
- }
- protected void onActivityResult(int requestCode, int resultCode,
- Intent intent) {
- super.onActivityResult(requestCode, resultCode, intent);
- if (resultCode == RESULT_OK) {
- Uri imageFileUri = intent.getData();
- Display currentDisplay = getWindowManager().getDefaultDisplay();
- int dw = currentDisplay.getWidth();
- int dh = currentDisplay.getHeight() / 2 - 100;
- try {
- BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
- bmpFactoryOptions.inJustDecodeBounds = true;
- Bitmap bmp = BitmapFactory
- .decodeStream(getContentResolver().openInputStream(
- imageFileUri), null, bmpFactoryOptions);
- int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
- / (float) dh);
- int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
- / (float) dw);
- if (heightRatio > 1 && widthRatio > 1) {
- if (heightRatio > widthRatio) {
- bmpFactoryOptions.inSampleSize = heightRatio;
- } else {
- bmpFactoryOptions.inSampleSize = widthRatio;
- }
- }
- bmpFactoryOptions.inJustDecodeBounds = false;
- bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
- Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
- .getHeight(), bmp.getConfig());
- Canvas canvas = new Canvas(alteredBitmap);
- Paint paint = new Paint();
- ColorMatrix cm = new ColorMatrix();
- // Increase Contrast, Slightly Reduce Brightness
- float contrast = 3;
- float brightness = 3;
- cm.set(new float[] { contrast, 0, 0, 0, brightness, 0,
- contrast, 0, 0, brightness, 0, 0, contrast, 0,
- brightness, 0, 0, 0, 1, 0 });
- paint.setColorFilter(new ColorMatrixColorFilter(cm));
- Matrix matrix = new Matrix();
- canvas.drawBitmap(bmp, matrix, paint);
- ImageView alteredImageView = (ImageView) this
- .findViewById(R.id.AlteredImageView);
- alteredImageView.setImageBitmap(alteredBitmap);
- chosenImageView.setImageBitmap(bmp);
- } catch (Exception e) {
- Log.v("ERROR", e.toString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement