Wednesday, 11 November 2020

Reset applied Blur effect in Android

Here i am using Freestyle BLUR now i want to undo that blur and set original image anyone have any idea Please tell me I have searched and got solution but not proper solution so please give me solution.

Here i am adding my activity where i give the blur effect please look once and give best solution.

public class BlurActivity extends AppCompatActivity {

private ImageButton opacityBtn,save_btn;
// custom view
private DrawingView drawView;
private Object bmpimg;
private ImageButton currPaint;
Bitmap b,ResetBitmap;
private int originalheight;
private int originalwidth;
public Bitmap tem,mutableBitmap;
Uri uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blur2);

    Bundle b11 = getIntent().getExtras();
    if (b11 != null) {
        String uri_Str = b11.getString("uri_Str");
        uri = Uri.parse(uri_Str);
    }


    try {
        final InputStream inputStream = getContentResolver().openInputStream(uri);
        b = BitmapFactory.decodeStream(inputStream);
        Log.e("abcd", "abcd" + uri + b);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


    // get button
    opacityBtn = (ImageButton) findViewById(R.id.opacity_btn);
    save_btn = (ImageButton) findViewById(R.id.save_btn);

    save_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            saveImage();

        }
    });
    // listen
    opacityBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // launch opacity chooser
            final Dialog seekDialog = new Dialog(BlurActivity.this);
            seekDialog.setTitle("Opacity level:");
            seekDialog.setContentView(R.layout.opacity_chooser);
            // get ui elements
            final TextView seekTxt = (TextView) seekDialog
                    .findViewById(R.id.opq_txt);
            final SeekBar seekOpq = (SeekBar) seekDialog
                    .findViewById(R.id.opacity_seek);
            // set max
            seekOpq.setMax(40);

            // show current level
            int currLevel = drawView.getPaintAlpha();
            seekTxt.setText(currLevel + "%");
            seekOpq.setProgress(currLevel);
            // update as user interacts
            seekOpq.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                                              boolean fromUser) {
                    seekTxt.setText(Integer.toString(progress) + "%");
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                }

            });
            // listen for clicks on ok
            Button opqBtn = (Button) seekDialog.findViewById(R.id.opq_ok);
            opqBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bitmap bitmap = createBitmap_ScriptIntrinsicBlur(tem, seekOpq.getProgress());
                    drawView.setPaintAlpha(seekOpq.getProgress(), bitmap);
                    seekDialog.dismiss();
                }

            });
            // show dialog
            seekDialog.show();
        }
    });



    drawView = (DrawingView) findViewById(R.id.drawing);

    // fetching height and width of device
    int widthPx = getWindowManager().getDefaultDisplay().getWidth();
    int heightPx = getWindowManager().getDefaultDisplay().getHeight();
    // set new height and width to custom class drawing view
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) drawView
            .getLayoutParams();


    Log.e("kskskskdjd","ldldldldld"+b.getHeight()+"--"+heightPx+"--"+b.getWidth()+"--"+widthPx);

    if (b.getHeight() < heightPx && b.getWidth() < widthPx) {
        params.height = b.getHeight();
        params.width = b.getWidth();
    } else {

        if (b.getHeight() > heightPx && b.getWidth() > widthPx) {

            params.height = heightPx;
            params.width = widthPx;
        } else if (b.getWidth() > widthPx) {
            params.width = widthPx;
            params.height = b.getHeight();

        } else {
            params.width = b.getWidth();
            params.height = heightPx;

        }
    }

    drawView.setLayoutParams(params);
    ResetBitmap = b;
    drawView.setCanvasBitmap(b, b.getHeight(), b.getWidth(), widthPx, heightPx);

    if (b.getHeight() < heightPx && b.getWidth() < widthPx) {
        this.originalheight = b.getHeight();
        this.originalwidth = b.getWidth();
    } else {

        if (b.getHeight() > heightPx && b.getWidth() > widthPx) {

            this.originalheight = heightPx;
            this.originalwidth = widthPx;
        } else if (b.getWidth() > widthPx) {
            this.originalwidth = widthPx;
            this.originalheight = b.getHeight();

        } else {
            this.originalwidth = b.getWidth();
            this.originalheight = heightPx;

        }
    }


    tem = Bitmap.createScaledBitmap(b, widthPx,
            heightPx, true);

    Bitmap bitmap = createBitmap_ScriptIntrinsicBlur(tem, 40);
    drawView.firstsetupdrawing(bitmap);

}


/*
 * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
 * menu; this adds items to the action bar if it is present.
 * getMenuInflater().inflate(R.menu.main, menu); return true; }
 */


public Bitmap createBitmap_ScriptIntrinsicBlur(Bitmap src, int radius) {
    // TODO Auto-generated method stub

    // TODO Auto-generated method stub

    // Radius range (0 < r <= 25)
/*  if (r <= 1) {
        r = 1;
    } else if (r > 25) {
        r = 25;
    }

    Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
            Bitmap.Config.ARGB_8888);

    RenderScript renderScript = RenderScript.create(this);

    Allocation blurInput = Allocation.createFromBitmap(renderScript, src);
    Allocation blurOutput = Allocation.createFromBitmap(renderScript,
            bitmap);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
            Element.U8_4(renderScript));
    blur.setInput(blurInput);
    blur.setRadius(r);
    blur.forEach(blurOutput);

    blurOutput.copyTo(bitmap);
    renderScript.destroy();
    return bitmap;*/

    int w = src.getWidth();
    int h = src.getHeight();
    int[] pix = new int[w * h];
    src.getPixels(pix, 0, w, 0, 0, w, h);

    for (int r = radius; r >= 1; r /= 2) {
        for (int i = r; i < h - r; i++) {
            for (int j = r; j < w - r; j++) {
                int tl = pix[(i - r) * w + j - r];
                int tr = pix[(i - r) * w + j + r];
                int tc = pix[(i - r) * w + j];
                int bl = pix[(i + r) * w + j - r];
                int br = pix[(i + r) * w + j + r];
                int bc = pix[(i + r) * w + j];
                int cl = pix[i * w + j - r];
                int cr = pix[i * w + j + r];

                pix[(i * w) + j] = 0xFF000000 |
                        (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) +
                                (br & 0xFF) + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF |
                        (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00)
                                + (br & 0xFF00) + (bc & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00 |
                        (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) +
                                (bl & 0xFF0000) + (br & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000) +
                                (cr & 0xFF0000)) >> 3) & 0xFF0000;
            }
        }
    }
    Bitmap blurred = Bitmap.createBitmap(w, h, src.getConfig());
    blurred.setPixels(pix, 0, w, 0, 0, w, h);
    return blurred;


}

private void saveImage() {

        direct.mkdir();
        Random generator = new Random();
        int n;
        n = generator.nextInt();
        String filename = "Images" + n + ".jpg";
        file = new File(direct, filename);
        Log.e("::SARTHAK::", "::file:: " +file +mutableBitmap);

        try {
            FileOutputStream mFileOutputStream = new FileOutputStream(file, false);
            bmap.compress(Bitmap.CompressFormat.JPEG, 100, mFileOutputStream);
            mFileOutputStream.flush();
            mFileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.e("::SARTHAK::", "::FileNotFoundException:: " + e.getMessage()+mutableBitmap);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("::SARTHAK::", "::IOException:: " + e.getMessage());
        }

        MediaScannerConnection.scanFile(BlurActivity.this, new String[]{file.toString()}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

    }
}

 }


from Reset applied Blur effect in Android

No comments:

Post a Comment