[Android Application Development]-(13) screenshot function-captures full screen without the need for Root (source code included)

Source: Internet
Author: User

You have to take a picture of the screen (of course, you can also take a picture to achieve your goal ). As a result, Google finds some applications that require Root permissions and do not require Root permissions, which are somewhat disappointing and mostly unavailable. So I want to develop an application on my own. The getDrawingCache method is provided in the View. You can obtain the screenshot of the View by using the second method, but only by capturing the View. What if I want to intercept the status bar?

In fact, the SystemUI in ICS implements the function. Press the combination key Power + Volume Add/Volume sub to intercept the image. Code directory:
Frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/contains two files in this directory. The main method is GlobalScreenshot, this article porting the code implementation function in SystemUI.

The first step is to directly transplant the SystemUI code to achieve the effect. This part of the code will not be posted. Download the Code directly. There are few key words in the Code. The most important thing is: Surface. screenshot (). Check the code.

[Java]
<SPAN style = "FONT-SIZE: 16px"> package org. winplus. ss;
 
Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
 
Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Matrix;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. util. Log;
Import android. view. Display;
Import android. view. Surface;
Import android. view. WindowManager;
Import android. OS. SystemProperties;
 
Public class SimpleScreenshotActivity extends Activity {
 
Private Display mDisplay;
Private WindowManager mWindowManager;
Private DisplayMetrics mDisplayMetrics;
Private Bitmap mScreenBitmap;
Private Matrix mDisplayMatrix;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
New Thread (new Runnable (){
 
@ Override
Public void run (){
TakeScreenshot ();
 
}
}). Start ();
}
 
Private float getDegreesForRotation (int value ){
Switch (value ){
Case Surface. ROTATION_90:
Return 360f-90f;
Case Surface. ROTATION_180:
Return 360f-180f;
Case Surface. ROTATION_270:
Return 360f-270f;
}
Return 0f;
}
 
Private void takeScreenshot (){
MWindowManager = (WindowManager) getSystemService (Context. WINDOW_SERVICE );
MDisplay = mWindowManager. getdefadisplay display ();
MDisplayMetrics = new DisplayMetrics ();
MDisplay. getRealMetrics (mDisplayMetrics );
MDisplayMatrix = new Matrix ();
Float [] dims = {mDisplayMetrics. widthPixels,
MDisplayMetrics. heightPixels };
 
Int value = mDisplay. getRotation ();
String hwRotation = SystemProperties. get ("ro. sf. hwrotation", "0 ");
If (hwRotation. equals ("270") | hwRotation. equals ("90 ")){
Value = (value + 3) % 4;
}
Float degrees = getDegreesForRotation (value );
 
Boolean requiresRotation = (degrees> 0 );
If (requiresRotation ){
// Get the dimensions of the device in its native orientation
MDisplayMatrix. reset ();
MDisplayMatrix. preRotate (-degrees );
MDisplayMatrix. mapPoints (dims );
 
Dims [0] = Math. abs (dims [0]);
Dims [1] = Math. abs (dims [1]);
}
 
MScreenBitmap = Surface. screenshot (int) dims [0], (int) dims [1]);
 
If (requiresRotation ){
// Rotate the screenshot to the current orientation
Bitmap ss = Bitmap. createBitmap (mDisplayMetrics. widthPixels,
MDisplayMetrics. heightPixels, Bitmap. Config. ARGB_8888 );
Canvas c = new Canvas (ss );
C. translate (ss. getWidth ()/2, ss. getHeight ()/2 );
C. rotate (degrees );
C. translate (-dims [0]/2,-dims [1]/2 );
C. drawBitmap (mScreenBitmap, 0, 0, null );
C. setBitmap (null );
MScreenBitmap = ss;
}
 
// If we couldn't take the screenshot, please y the user
If (mScreenBitmap = null ){
Return;
}
 
// Optimizations
MScreenBitmap. setHasAlpha (false );
MScreenBitmap. prepareToDraw ();

Try {
SaveBitmap (mScreenBitmap );
} Catch (IOException e ){
System. out. println (e. getMessage ());
}
}
 
Public void saveBitmap (Bitmap bitmap) throws IOException {
String imageDate = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss ")
. Format (new Date (System. currentTimeMillis ()));
File file = new File ("/mnt/sdcard/Pictures/" + imageDate + ". png ");
If (! File. exists ()){
File. createNewFile ();
}
FileOutputStream out;
Try {
Out = new FileOutputStream (file );
If (bitmap. compress (Bitmap. CompressFormat. PNG, 70, out )){
Out. flush ();
Out. close ();
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
</SPAN>

Package org. winplus. ss;

Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Date;

Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Matrix;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. util. Log;
Import android. view. Display;
Import android. view. Surface;
Import android. view. WindowManager;
Import android. OS. SystemProperties;

Public class SimpleScreenshotActivity extends Activity {

Private Display mDisplay;
Private WindowManager mWindowManager;
Private DisplayMetrics mDisplayMetrics;
Private Bitmap mScreenBitmap;
Private Matrix mDisplayMatrix;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

New Thread (new Runnable (){

@ Override
Public void run (){
TakeScreenshot ();

}
}). Start ();
}

Private float getDegreesForRotation (int value ){
Switch (value ){
Case Surface. ROTATION_90:
Return 360f-90f;
Case Surface. ROTATION_180:
Return 360f-180f;
Case Surface. ROTATION_270:
Return 360f-270f;
}
Return 0f;
}

Private void takeScreenshot (){
MWindowManager = (WindowManager) getSystemService (Context. WINDOW_SERVICE );
MDisplay = mWindowManager. getdefadisplay display ();
MDisplayMetrics = new DisplayMetrics ();
MDisplay. getRealMetrics (mDisplayMetrics );
MDisplayMatrix = new Matrix ();
Float [] dims = {mDisplayMetrics. widthPixels,
MDisplayMetrics. heightPixels };

Int value = mDisplay. getRotation ();
String hwRotation = SystemProperties. get ("ro. sf. hwrotation", "0 ");
If (hwRotation. equals ("270") | hwRotation. equals ("90 ")){
Value = (value + 3) % 4;
}
Float degrees = getDegreesForRotation (value );

Boolean requiresRotation = (degrees> 0 );
If (requiresRotation ){
// Get the dimensions of the device in its native orientation
MDisplayMatrix. reset ();
MDisplayMatrix. preRotate (-degrees );
MDisplayMatrix. mapPoints (dims );

Dims [0] = Math. abs (dims [0]);
Dims [1] = Math. abs (dims [1]);
}

MScreenBitmap = Surface. screenshot (int) dims [0], (int) dims [1]);

If (requiresRotation ){
// Rotate the screenshot to the current orientation
Bitmap ss = Bitmap. createBitmap (mDisplayMetrics. widthPixels,
MDisplayMetrics. heightPixels, Bitmap. Config. ARGB_8888 );
Canvas c = new Canvas (ss );
C. translate (ss. getWidth ()/2, ss. getHeight ()/2 );
C. rotate (degrees );
C. translate (-dims [0]/2,-dims [1]/2 );
C. drawBitmap (mScreenBitmap, 0, 0, null );
C. setBitmap (null );
MScreenBitmap = ss;
}

// If we couldn't take the screenshot, please y the user
If (mScreenBitmap = null ){
Return;
}

// Optimizations
MScreenBitmap. setHasAlpha (false );
MScreenBitmap. prepareToDraw ();

Try {
SaveBitmap (mScreenBitmap );
} Catch (IOException e ){
System. out. println (e. getMessage ());
}
}

Public void saveBitmap (Bitmap bitmap) throws IOException {
String imageDate = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss ")
. Format (new Date (System. currentTimeMillis ()));
File file = new File ("/mnt/sdcard/Pictures/" + imageDate + ". png ");
If (! File. exists ()){
File. createNewFile ();
}
FileOutputStream out;
Try {
Out = new FileOutputStream (file );
If (bitmap. compress (Bitmap. CompressFormat. PNG, 70, out )){
Out. flush ();
Out. close ();
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

PS: 1. You need to add the code in AndroidManifest. xml: android: sharedUserId = "android. uid. system"

2. Because @ hide API is called, use makefile for compiling. Or add a Jar file in Eclipse and compile it.

3. This code has been used only in Android4.0, and 2.3 has not been tested.

Author: tangcheng_ OK
 

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.