Android Multimedia Framework Summary (25) mediaprojection for mobile phone screenshot (no root required)

Source: Internet
Author: User
Tags root access

Reprint please the head source link and the tail two-dimensional code together reprint, this article from countercurrent fish yuiop:http://blog.csdn.net/hejjunlin/article/details/53966818

Foreword: More than 1.5 ago, we had a project, to do a screenshot function, was responsible for research colleagues, agreed to the product on this feature, but after a week of development, found that can not achieve the interception of mobile phone screen images, the need for root access to do. Because the recent research mediaprojection, unexpectedly discovered, unexpectedly does not need root, can easily realize the sub-function. Once can not be done, now do, it is difficult to believe the mood at this time. Look at today's agenda:

    • How does the use of key combinations in Android source code make the screen function?
    • Mediaprojection to achieve the screen effect of mobile phone
    • Brief ideas

Take my Meizu mobile phone as an example, is at the same time press the Power key + volume down button to achieve the screenshot, the Apple phone is the power button + Home button, Xiaomi phone is the menu key + volume down key, and HTC is generally press and hold the Power key and press the bottom left corner of the "Home" key. So how to use the combination key in the Android source code to implement the screen function?

The capture of keys in Android source is in file Phonewindowmanager.java




This class handles all key events, where the function interceptkeybeforequeueing () makes special handling of commonly used keys. Take my own Meizu phone as an example, the same time by pressing the power key and the volume down button to screen, then in this method can see the following code:
Interceptkeybeforequeueing



The above code summarizes: when the volume down button is pressed, and is down, will enter into the Interceptpowerkeydown method, and when the power key is pressed, and is down, will also enter the Interceptpowerkeydown method, Then go into this method to see:
Interceptpowerkeydown



The above code summarizes: If Interactive is true, and some other conditions are met, the time of the Power Key press is recorded and entered in the Interceptscreenshotchord method:



The above code summarizes: With two Boolean variables to determine whether the volume is pressed at the same time the key and the Power key, and then calculate the two keys response to the difference between the down event is not more than (//time to volume and power must is pressed within this Interva L of each of the other.
Private static final long screenshot_chord_debounce_delay_millis = 150;) 150 milliseconds, it is considered that both keys are pressed at the same time, this case, is the real screen screenshot of the combination of keys.

After getting to the key combination, a mhandler begins post with a runnable to enter the runnable:




The above code executes the method Takescreenshot method and then enters:



The above code concludes that the service service is bound to "Com.android.systemui.screenshot.TakeScreenshotService" using Aidl, and that when the service connection is successful, Assigns a value to the MSG.ARG1 and msg.arg2 two parameters of the message. In Mscreenshottimeout, service services have been processed over time. Then we find the class Takescreenshotservice that implements the service, which is in the (frameworks/base/packages/systemui/src/com/android/systemui/ Screenshot package):



The corresponding code is as follows:



The above code summarizes as follows: The Globalscreenshot class is referenced, the Takescreenshot method is called, followed by the entry:



The above code summarizes: Reference Surfacecontrol class, called the screenshot method, passed in the screen width and height, these two parameters, and then into the Surfacecontrol class, located in the frameworks/base/core/java/ Under the Android/view directory:



The corresponding screenshot method code is as follows:



Screenshot



Finally arrives in the native method nativescreenshot



Above is the Java layer, then to the JNI layer, in \frameworks\base\core\jni\android_view_surfacecontrol.cpp:



The above code summarizes: to JNI, the mapping Nativescreenshot method is the Nativescreenshotbitmap function, this function code is as follows:




The above code is summarized as follows: Instantiate the Screenshotclient class, and call the Update method, finally through the construction of a new bitmap, the screenshot obtained pixel, and related information reached bitmap, through Graphicsjni, Re-create a bitmap to return to the upper layer.
Let's take a look at the Screenshotclient statement, located in SurfaceComposerClient.h. Note that this class is in Frameworks\native\include\gui, while the front android_view_surfacecontrol is under \frameworks\base\core\jni\



Finally came to the C + + layer, is \frameworks\native\libs\gui under the SurfaceComposerClient.cpp, implementation of Screenshotclient declaration function update, as follows:



The above code summarizes: through the Isurfacecomposer interface, call the Capturescreen function, we find its corresponding implementation class \frameworks\native\libs\gui\ ISurfaceComposer.cpp, find the implementation of the Capturescreen function as follows:



The above code summarizes: The Igraphicbufferproducer, as Binder began to write to parcel, the last version to see Transact, know that the current client side, and the server side of the Ontransact () function, will receive a pass-through parameter. As the data here. Then look at the Ontransact method of Bnsurfacecomposer, the Bnsurfacecomposer is still in the ISurfaceComposer.cpp.



The above code summarizes: When entered into the Capture_screen, data will read the igraphicbufferproducer generated image Buffe, and then call Reply->writeint32 (RES); Back to the client. Then callback to the Java layer. The above is the system of the principle of screen cutting.

That for the multimedia this can be achieved through the Mediaprojection+virturaldisplay+mediaprojectionmanager to achieve screenshots, the following my example:

1: Operation process, click on the floating small scissors, you can achieve the screenshot, drag the scissors, can be any position:



2: Post-process



Main interface:



Experience APK:
Link: Http://pan.baidu.com/s/1kVugxCV Password: 97z0

Implementation ideas:

    • Mediaprojection is a token that the user can grant the application to capture screen content and record system audio.
    • Screen capture, you can create a intent through the Mediaprojectionmanager createscreencaptureintent, to ensure that there is sufficient ability to intercept the content on the screen, but there is no system sound.
    • Call the Setupmediaprojection () method to obtain and assign the shared data, or, if initialized, directly call the Virtualdisplay () method to initialize the screenshot environment with the previously defined variables.

The first time to get blog update reminders, as well as more Android dry, source code Analysis , Welcome to follow my public number, sweep the bottom QR code or long press to identify two-dimensional code, you can pay attention to.


If you feel good, easy to praise, but also to the author's affirmation, can also share this public number to you more people, original not easy

Android Multimedia Framework Summary (25) mediaprojection for mobile phone screenshot (no root required)

Related Article

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.