THE PROBLEM
Trying to adjust saturation brigthness and contrast on a bitmap with a seekbar cause an outofmemory error.
RELEVANT CODE
Some variables:
private SeekBar seekbar;
private int saturation = 0;
private int contrast = 50;
private int brightness = 50;
private ImageView imageView1;
private Bitmap originalImageBitmap;
private Bitmap modBitmap;
private Canvas c;
private Uri data;
in my onCreate method:
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor_foto);
// Get Image uri from the previous activity and set an ImageView
Intent intent = getIntent();
data = intent.getData();
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageURI(data);
// Get the bitmap from the ImageView
Drawable drawable = imageView1.getDrawable();
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
originalImageBitmap = bitmapDrawable.getBitmap();
bBrightness = (ImageButton)findViewById(R.id.brightness);
bContrast = (ImageButton)findViewById(R.id.contrast);
bSaturation = (ImageButton)findViewById(R.id.saturation);
seekbar = (SeekBar)findViewById(R.id.seekbar);
c = new Canvas();
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
if (bBrightnessPressed) {
brightness = progress;
}
if (bContrastPressed) {
contrast = progress;
}
if (bSaturationPressed) {
saturation = progress;
}
modBitmap = changeColorBrightnessContrast(originalImageBitmap, c, data, ((100-(float)saturation)/100), (brightness-50), ((100-(float)contrast)/100));
imageView1.setImageBitmap(modBitmap);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
...
and the method changeColorBrightnessContrast
public static Bitmap changeColorBrightnessContrast(Bitmap src, Canvas c, Uri data, float colore, float brightness, float contrast) {
int width, height;
height = src.getHeight();
width = src.getWidth();
Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final float GS_RED = 0.299f;
final float GS_GREEN = 0.587f;
final float GS_BLUE = 0.114f;
float sr = (colore) * GS_RED;
float sg = (colore) * GS_GREEN;
float sb = (colore) * GS_BLUE;
c.setBitmap(bmOut);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix(new float[]
{
(sr+(1-colore))+contrast, sg, sb, 0, brightness,
sr, (sg+(1-colore))+contrast, sb, 0, brightness,
sr, sg, (sb+(1-colore))+contrast, 0, brightness,
0, 0, 0, 1, 0
});
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(src, 0, 0, paint);
return bmOut;
}
The adjustment method is build with these image processing tutorials like reference. In a previous version the c = new Canvas();
was inside the changeColorBrightnessContrast method, I've read that this may launch this exeption so I moved this line outside in the onCreate method.
The bitmap in the imageView came from the gallery or the camera in another activity and passed to this, but is previously resize to 640px in width.
Thank for any ideas
0 comments:
Post a Comment