4

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.

user2688158
  • 407
  • 5
  • 17

1 Answers1

2

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.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • Thanks for your response. So for the 2nd question should I pass the value 'brightness' like Intent.putExtra("image_brightness",brightness) ? – user2688158 Feb 26 '14 at 03:35
  • That's correct. I was also thinking to put the image `int[]` raw data instead directly, but I'm afraid it's not efficient and can cause out of memory or other problems. – Andrew T. Feb 26 '14 at 03:38
  • doBrightness method is defined in Activity B and on a button click I pass the brighness as Intent.putExtra("image_brightness",brightness). In activity A is it right to do putGestureImageOnScreen(Activity B.doBrightness(imageBitmap, brightness)); ? – user2688158 Feb 26 '14 at 03:50
  • I just tried it and I get NullPointer Exception.. I'm not sure if its because of the way I call the method doBrightness in Activity A. But the method is public and the Activity B is also public. – user2688158 Feb 26 '14 at 03:52
  • 1
    Ah, I think you also need to pass the reference to the original `Bitmap` first (filename? URI?). Basically, you will do similar thing like in Activity B; load the original image as `Bitmap` and do the manipulation. I'm sure you can call `ActivityB.doBrightness(...)` (though maybe you can move the code to utility class), the problem might be `imageBitmap` is null or not referenced. Try to debug on Activity A first, or create a new question which include the stack trace. – Andrew T. Feb 26 '14 at 03:59
  • I have posted a question related to the above problem. http://stackoverflow.com/questions/22032914/not-able-to-get-rid-of-nullpointerexception-when-changing-brightness-of-an-image. Can you please have a look. Thanks. – user2688158 Feb 26 '14 at 05:51
  • Also in the above SeekBar how can I set the scrolling cursor to start from the middle. Like Set it to zero in the middle. Move left to decrease the brightness and right to increase the brightness – user2688158 Feb 26 '14 at 05:56
  • `SeekBar` doesn't accept negative value, though you can "trick" it by having range from 0 to 511, set the starting position to 255, then always subtract the value by 255 before processing it (e.g. `brightness = progress - 255;`. Have a look a [this question](http://stackoverflow.com/questions/15471508/seekbar-with-negative-values-on-android) too. – Andrew T. Feb 26 '14 at 06:03
  • @AndrewT. can you show me putGestureImageOnScreen method? – Piyush Mar 20 '14 at 07:32
  • @PiYusHGuPtA sorry, I don't have the code for that. I just use the function already provided by OP in his question. You may ask OP instead. – Andrew T. Mar 20 '14 at 07:41
  • Okay. Thanks any way. I have wandered for that method. – Piyush Mar 20 '14 at 07:42