In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to achieve screenshot function in Android". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve screenshot function in Android" can help you solve your doubts.
Introduction
The screenshot API opened after Android 5.0can capture a frame of data in the video, so that the screenshot can be realized.
Steps
Authorization in activity, initialization and screenshot in service. Of course, screenshots can be taken regularly in the background, but 6. 0 system will have bug with memory overflow.
1:build.gradle
CompileSdkVersion 21 buildToolsVersion "27.0.3" defaultConfig {applicationId "com.aile.screenshot" multiDexEnabled true minSdkVersion 21 targetSdkVersion 21 versionCode 1 versionName "1.0"}
2: authorize in activity
Public void requestCapturePermission () {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {return;} MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService (Context.MEDIA_PROJECTION_SERVICE); startActivityForResult (mediaProjectionManager.createScreenCaptureIntent (), REQUEST_MEDIA_PROJECTION);} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data) Switch (requestCode) {case REQUEST_MEDIA_PROJECTION: if (resultCode = = RESULT_OK & & data! = null) {Service.setResultData (data); startService (new Intent (this, Service.class)); finish ();} break;}}
3: initialize ImageReader,MediaProjection in service
Private void createImageReader () {mImageReader = ImageReader.newInstance (mScreenWidth, mScreenHeight, PixelFormat.RGBA_8888, 1);} public void setUpMediaProjection () {mMediaProjection = getMediaProjectionManager () .getMediaProjection (Activity.RESULT_OK, mResultData);}}
4: complete the important steps of screenshot in service:
Private void startScreenShot () {Handler handler = new Handler (); handler.postDelayed (new Runnable () {@ Override public void run () {startVirtual ();}}, 0); handler.postDelayed (new Runnable () {@ Override public void run () {startCapture () }, 50);} public void startVirtual () {if (mMediaProjection! = null) {virtualDisplay ();} else {setUpMediaProjection (); virtualDisplay () }} private void virtualDisplay () {mVirtualDisplay = mMediaProjection.createVirtualDisplay ("screen-mirror", mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface (), null, null);} / / Core private void startCapture () for exception handling {Image image = null; try {image = mImageReader.acquireLatestImage () } catch (IllegalStateException e) {if (null! = image) {image.close (); image = null; image = mImageReader.acquireLatestImage ();}} if (image = = null) {startScreenShot ();} else {SaveTask mSaveTask = new SaveTask () AsyncTaskCompat.executeParallel (mSaveTask, image); new Handler () .postDelayed (new Runnable () {@ Override public void run () {stopVirtual (); tearDownMediaProjection ();}}, 0) }} public class SaveTask extends AsyncTask {@ Override protected Bitmap doInBackground (Image... Params) {if (params = = null | | params.length < 1 | | params [0] = = null) {return null;} Image image = params [0]; int width = image.getWidth (); int height = image.getHeight (); final Image.Plane [] planes = image.getPlanes (); final ByteBuffer buffer = planes [0] .getBuffer () Int pixelStride = planes [0] .getPixelStride (); int rowStride = planes [0] .getRowStride (); int rowPadding = rowStride-pixelStride * width; Bitmap bitmap = Bitmap.createBitmap (width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer (buffer); / / this is the initial screenshot bitmap = Bitmap.createBitmap (bitmap, 0, 0, width, height) Image.close (); return bitmap;} @ Override protected void onPostExecute (final Bitmap bitmap) {super.onPostExecute (bitmap); / / deal with the business code of bitmap}
5:Bitmap is converted to IS stream, and screenshots of specified areas are taken.
/ / convert Bitmap to InputStream ByteArrayOutputStream bos = new ByteArrayOutputStream (); bitmap.compress (Bitmap.CompressFormat.PNG, 100,bos); InputStream inputStream = new ByteArrayInputStream (bos.toByteArray ()); / / Screenshot of specified area Rect mRect = new Rect (51,74,58,62); BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance (inputStream, true); Bitmap bm = bitmapRegionDecoder.decodeRegion (mRect, null)
6: processing of scheduled tasks
Private Timer timer = new Timer (); public void shootByTime () {final Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {startScreenShot (); super.handleMessage (msg);}} Timer.schedule (new TimerTask () {@ Override public void run () {Message message = new Message (); message.what = 1; handler.sendMessage (message);}}, 0100);}
7: horizontal and vertical screen processing
@ Override public void onConfigurationChanged (Configuration newConfig) {super.onConfigurationChanged (newConfig); if (newConfig.orientation = = this.getResources (). GetConfiguration (). ORIENTATION_PORTRAIT) {mRect = new Rect (51,775,745,47);} else if (newConfig.orientation = = this.getResources (). GetConfiguration (). ORIENTATION_LANDSCAPE) {mRect = new Rect (54,24,545,45) }} after reading this, the article "how to achieve screenshot in Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.