Screenshot, Blur, transfer, and use of Android bitmap images

Source: Internet
Author: User

A requirement was encountered in the project:

When a condition is satisfied, it intercepts the current screen and jumps to another page, while the screenshot is the background image of the next page, and the background image needs to be blurred.

The next step is to solve the problem:

1. To intercept the current screen picture without the status bar, please refer to the Takescreenshot method

2, make the picture Gaussian blur method, please refer to Blurbitmap method

Note: Renderscript is an Android added after API 11 for efficient image processing, including blur, blending, matrix convolution calculations, and more

public class Screenshotutil {//Get a screenshot of the specified activity, save to PNG file String filenametemp = "/mnt/sdcard/temp"; /** * Takescreenshot: * TODO screenshot Remove title bar * @param activity */public static Bitmap Takescreenshot (Activ        ity activity) {//view is your desired view View view = Activity.getwindow (). Getdecorview ();        View.setdrawingcacheenabled (TRUE);        View.builddrawingcache ();        Bitmap B1 = View.getdrawingcache ();        Gets the status bar height rect frame = new rect ();        Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);        int statusbarheight = Frame.top;        LOGHELPER.I ("TAG", "" + statusbarheight);        Gets the screen length and height int width = activity.getwindowmanager (). Getdefaultdisplay (). GetWidth ();        int height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();        Remove title bar//Bitmap B = Bitmap.createbitmap (B1, 0, 25, 320, 455); Bitmap B = Bitmap.createbitmap (b1, 0, Statusbarheight, width, height-sTatusbarheight);        View.destroydrawingcache ();    return b;    }/** * TODO for efficient image processing, including blur, mix, matrix convolution calculations, etc. * @param bitmap * @param context */@SuppressLint ("Newapi") public static Bitmap Blurbitmap (Bitmap Bitmap, Context context) {/-let's create an empty Bitmap with the same Size of the bitmap we want//to blur bitmap Outbitmap = Bitmap.createbitmap (Bitmap.getwidth (), bitmap.gethe        Ight (), config.argb_8888);         Instantiate a new renderscript renderscript rs = renderscript.create (context);//renderscript is Android added after API 11 Create an intrinsic Blur Script using the renderscript scriptintrinsicblur blurscript = SCRIPTINTRINSICB        Lur.create (RS, Element.u8_4 (RS)); Create the Allocations (in/out) with the Renderscript and the in/out//bitmaps Allocation Allin = Alloca        Tion.createfrombitmap (rs, bitmap); Allocation Allout = Allocation.createfrombitmap (RS, outbitmap);       Set the radius of the Blur Blurscript.setradius (25.F);        Perform the Renderscript blurscript.setinput (Allin);        Blurscript.foreach (Allout);        Copy the final bitmap created by the-Allocation to the Outbitmap Allout.copyto (OUTBITMAP);        Recycle the original bitmap bitmap.recycle ();        After finishing everything, we destroy the Renderscript.        Rs.destroy ();    return outbitmap; }}

3, Transmission bitmap

That's how I passed it at first.

Bundle.putparcelable ("Bitmap", Screenshotutil.takescreenshot (Thelayout.getactivity ()));

Proceed as follows: encapsulating the bitmap into the bundle and then encapsulating it into the intent to start the next activity

Activityutil.startactivity (Thelayout.getactivity (), Liveendactivity.class, bundle, false);

/**     * Open another activity     *      * @param activity     * @param CLS Additional activity class     * @param bundle Objects     passed by bundles * @param isfinish True indicates that you want to close activity false to not close activity     *    /public static void startactivity (activity activity, Class<?> CLS, bundle Bundle, Boolean isfinish) {        Intent Intent = new Intent (activity, CLS);        if (bundle! = null) {            Intent.putextras (bundle);        }        Activity.startactivity (intent);        if (isfinish) {            activity.finish ();        }        Activity.overridependingtransition (r.anim.push_left_in, r.anim.push_left_out);    }
And then in Liveendactivity.
Bitmap Bitmap = Intent.getextras (). Getparcelable ("Bitmap");
Result: Unable to achieve expected effect

The key is not error, debug when you can see that we do screenshot success, but the bitmap object is not passed the past, and not to start the next activity

Then go online and find ways to research.

Conclusion: Images larger than 40k cannot be transmitted directly.

Workaround: Store the bitmap as a byte array, and then continue passing

       Bitmap Bitmap = Screenshotutil.takescreenshot (Thelayout.getactivity ());       Bytearrayoutputstream baos=new Bytearrayoutputstream ();         Bitmap.compress (Bitmap.CompressFormat.PNG, BAOs);         byte [] bitmapbyte =baos.tobytearray ();         Bundle.putbytearray ("Bitmap", bitmapbyte);       Bundle.putparcelable ("Bitmap", Screenshotutil.takescreenshot (Thelayout.getactivity ()));                          Activityutil.startactivity (Thelayout.getactivity (), Liveendactivity.class, bundle, false);

And then parse it in the next activity.

byte[] bis =intent.getextras (). Getbytearray ("bitmap");            

4, if we need to set this image as our current activity background picture, we can do so

if (bitmap! = null) {       bitmap = Screenshotutil.blurbitmap (Bitmap,getapplicationcontext ());//Gaussian obfuscation       GetWindow ( ). Getdecorview (). setbackgrounddrawable (New bitmapdrawable (bitmap)); }

The problem is basically solved, feel useful can refer to!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Screenshot, Blur, transfer, and use of Android bitmap images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.