2

I got problem with my simple image editor. After contrast change I am trying to set new, edited image, but exception appears.

public ContrastBrightnessDialog(Context context, String title, final DrawingView drawView) {
    super(context);
    this.setContentView(R.layout.contrastbrightness_dialog);
    this.setTitle(title);

    txtView = (TextView) findViewById(R.id.textView1);
    contrastValue = (TextView) findViewById(R.id.contrast_value);
    brightnessValue = (TextView) findViewById(R.id.textView1);
    contrastSB = (SeekBar) findViewById(R.id.contrast_seekbar);
    contrastSB.setProgress(0);

    contrastSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // TODO Auto-generated method stub
                    contrastValue.setText(String.valueOf(progress - 100));
                    contrastInt = Integer.parseInt(String
                            .valueOf(progress - 100));
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                }
            });

    confirmBtn = (Button) findViewById(R.id.confirm_btn);
    confirmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    drawView.setDrawingCacheEnabled(true);
                    drawView.measure(
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    drawView.layout(0, 0, drawView.getMeasuredWidth(),
                            drawView.getMeasuredHeight());

                    drawView.buildDrawingCache(true);

                    Bitmap bmin = Bitmap.createBitmap(drawView.getDrawingCache());

                    BitmapFactory.Options options = new BitmapFactory.Options();

                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    Bitmap workingBitmap = Bitmap.createBitmap(doContrast(bmin,
                            contrastInt));

                    Bitmap mutableBitmap = workingBitmap.copy(
                            Bitmap.Config.ARGB_8888, true);
                    options = new BitmapFactory.Options();
                    Drawable d = new BitmapDrawable(drawView.getResources(),
                            mutableBitmap);
                    drawView.setBackground(d);
                    drawView.setDrawingCacheEnabled(false);
            close();
        }
    });
}

public Bitmap doContrast(Bitmap src, int value) {
 //contrast algorithm from https://xjaphx.wordpress.com/2011/06/21/image-processing-contrast-image-on-the-fly/
return someBitmap;}

And here is a method from my DrawingView class (extends ImageView), which gets and line with error. Debuger shows w and h are equal 0.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);//here is an error
    drawCanvas = new Canvas(canvasBitmap);
}
slavman
  • 43
  • 5
  • What line does the error appear on? Also, you don't appear to call the `onSizeChanged` method anywhere in your code. Why is it included? – The Guy with The Hat Jan 25 '15 at 21:10
  • It's called from ImageView class I think when new image will be drawed on ImageView object. I saw many exaples how to deal with that exception problem, but really I don't know how do that in my code. – slavman Jan 25 '15 at 21:19
  • see this optimized solution http://stackoverflow.com/a/31237347/185022 – AZ_ Jul 06 '15 at 03:25

2 Answers2

0

The error you are getting is usually caused when the IamgeView has no width/height assigned to it. If you are creating a new Image, then you must specify width and height of the View using LayoutParams.

Something like : mImageView.setLayoutParams(new LayoutParams(mwidth. mheight));

Swayam
  • 16,294
  • 14
  • 64
  • 102
0

I found an answer! I read that post: ShowcaseView - width and height must be > 0

And there is a working code:

confirmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            drawView.post(new Runnable() {
                @Override
                public void run() {
                    drawView.layout(0, 0, drawView.getMeasuredWidth(),
                            drawView.getMeasuredHeight());

                    drawView.setDrawingCacheEnabled(true);
                    drawView.buildDrawingCache(true);


                    Bitmap bmin = drawView.getDrawingCache();

                    BitmapFactory.Options options = new BitmapFactory.Options();

                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    Bitmap workingBitmap = Bitmap.createBitmap(doContrast(bmin,
                            c, b));

                    Bitmap mutableBitmap = workingBitmap.copy(
                            Bitmap.Config.ARGB_8888, true);

                    options = new BitmapFactory.Options();

                    Drawable d = new BitmapDrawable(drawView.getResources(),
                            mutableBitmap);

                    drawView.setBackground(d);

                    drawView.setDrawingCacheEnabled(false);

                    drawView.invalidate();
                }
            });

            close();
        }
    });
}
Community
  • 1
  • 1
slavman
  • 43
  • 5
  • this solution is far more optimized since it's being called from 2 places http://stackoverflow.com/a/31237347/185022 – AZ_ Jul 06 '15 at 03:25