Question

In my app, I want to be able to change the brightness of an image within an ImageView. I use seekbar for this purpose. It does change the brightness of the image when I move the scrollbar right but when I want to reduce the brightness and move left the brightness keeps increasing and the image become nearly white. Also it is so difficult to use the seekbar as a user. It doesn't move very smoothly. Can someone please help me to improve my code as I am a beginner in programming. This is how it looks when scrollbar of seekbar moved right (to increase brightness).This is how it looks when scrollbar of seekbar moved left(to decrease brightness).

Here is part of the code:

On click of a button "filter" :

btn_filter.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
      Log.e("EditPhoto", "counter value = " + counter);
      counter++;
      sbarBrightness = (SeekBar) findViewById(R.id.seekBarForBrightness);
        if (counter % 2 == 0) {
            sbarBrightness.setVisibility(View.INVISIBLE);
        } else {
            sbarBrightness.setVisibility(View.VISIBLE);
        }
    sbarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
     int brightness;
     @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean
                                          fromUser) {

    brightness = progress;
    imageBitmap = doBrightness(imageBitmap, brightness);
    putGestureImageOnScreen(imageBitmap);
 }
   });
  }
 });

public static Bitmap doBrightness(Bitmap src, int value) {
    Log.e("Brightness", "Changing brightnhjh");

    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmout = Bitmap.createBitmap(width, height, src.getConfig());
    int A, R, G, B;
    int pixel;
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            pixel = src.getPixel(i, j);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            R += value;
            if (R > 255) {
                R = 255;
            } else if (R < 0) {
                R = 0;
            }
            G += value;
            if (G > 255) {
                G = 255;
            } else if (G < 0) {
                G = 0;
            }
            B += value;
            if (B > 255) {
                B = 255;
            } else if (B < 0) {
                B = 0;
            }
            bmout.setPixel(i, j, Color.argb(A, R, G, B));
        }
    }
    return bmout;

}

Seekbar's declaration in the layout:

<SeekBar
    android:id="@+id/seekBarForBrightness"
    android:layout_width="500dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    style="@style/tallerBarStyle"        
    android:layout_marginTop="64dp"
    android:visibility="invisible" />

and this its style is as below:

<style name="tallerBarStyle" parent="@android:style/Widget.SeekBar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
    <item
     name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
    <item name="android:minHeight">8dip</item>
    <item name="android:maxHeight">10dip</item>
</style>

Also I want to save the brightness level of the image such that when I pass the image to another activity, the brighness is not lost. Can someone please guide me, how can I achieve it.

Thanks.

Was it helpful?

Solution

The problem is caused by doing manipulation on the same Bitmap reference (imageBitmap = doBrightness(imageBitmap, brightness);). Hence, when you drag the slider to the left, you're still continuously increasing the brightness to the image.

Try

brightness = progress;
//imageBitmap = doBrightness(imageBitmap, brightness);
putGestureImageOnScreen(doBrightness(imageBitmap, brightness));

For your 2nd question, you can use Intent to transfer the brightness value to next Activity and do the brightness manipulation once to get the same result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top