Android屏幕截图实现
16lz
2021-01-25
方法一,根据android代码实现的流程在复制一遍,流程上一篇已经大概看过了,网上也有很多已经实现了。下面我就转载一篇
来自http://blog.csdn.net/hk_256
1. Activity文件
[java] view plain copy print ?- packagecom.arvinhe.testscreenshot;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.Canvas;
- importandroid.graphics.Matrix;
- importandroid.os.Bundle;
- importandroid.util.DisplayMetrics;
- importandroid.view.Display;
- importandroid.view.Surface;
- importandroid.view.View;
- importandroid.view.WindowManager;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.ImageView;
- publicclassTestScreenShotActivityextendsActivityimplementsOnClickListener{
- privateImageViewimg_display;
- privateButtonbt_screenshot;
- privateDisplaymDisplay;
- privateDisplayMetricsmDisplayMetrics;
- privateMatrixmDisplayMatrix;
- privateBitmapmScreenBitmap;
- privateWindowManagermWindowManager;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- bt_screenshot=(Button)findViewById(R.id.bt_screenshot);
- img_display=(ImageView)findViewById(R.id.img_display);
- bt_screenshot.setOnClickListener(this);
- mDisplayMatrix=newMatrix();
- mWindowManager=(WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
- mDisplay=mWindowManager.getDefaultDisplay();
- mDisplayMetrics=newDisplayMetrics();
- mDisplay.getRealMetrics(mDisplayMetrics);
- }
- @Override
- publicvoidonClick(Viewv){
- if(v.equals(bt_screenshot)){
- mDisplay.getRealMetrics(mDisplayMetrics);
- float[]dims={mDisplayMetrics.widthPixels,mDisplayMetrics.heightPixels};
- floatdegrees=getDegreesForRotation(mDisplay.getRotation());
- booleanrequiresRotation=(degrees>0);
- if(requiresRotation){
- //Getthedimensionsofthedeviceinitsnativeorientation
- 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){
- //Rotatethescreenshottothecurrentorientation
- Bitmapss=Bitmap.createBitmap(mDisplayMetrics.widthPixels,
- mDisplayMetrics.heightPixels,Bitmap.Config.ARGB_8888);
- Canvasc=newCanvas(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;
- }
- //Ifwecouldn'ttakethescreenshot,notifytheuser
- if(mScreenBitmap==null){
- return;
- }
- //Optimizations
- mScreenBitmap.setHasAlpha(false);
- mScreenBitmap.prepareToDraw();
- img_display.setImageBitmap(mScreenBitmap);
- }
- }
- /**
- *@returnthecurrentdisplayrotationindegrees
- */
- privatefloatgetDegreesForRotation(intvalue){
- switch(value){
- caseSurface.ROTATION_90:
- return360f-90f;
- caseSurface.ROTATION_180:
- return360f-180f;
- caseSurface.ROTATION_270:
- return360f-270f;
- }
- return0f;
- }
- }
2. AndroidManifest.xml文件
[html] view plain copy print ?- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.arvinhe.testscreenshot"
- android:versionCode="1"
- android:versionName="1.0"
- android:sharedUserId="android.uid.system">
- <uses-sdkandroid:minSdkVersion="15"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name">
- <activity
- android:name=".TestScreenShotActivity"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
3. Layout文件
[html] view plain copy print ?- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"/>
- <Button
- android:id="@+id/bt_screenshot"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="ScreenShot"
- />
- <ImageView
- android:id="@+id/img_display"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher"/>
- </LinearLayout>
来自:http://blog.csdn.net/ericahdu/article/details/5533091
我们有时候只是需要截图,没必要连DDMS一起开,所以剥离了截图的代码,当然,并不是原生的啊,是根据原理自己写的,供大家参考
第一步,准备库包
我们既然是按照DDMS的方法截图,就需要用到ddmlib.jar这个包,它位于android的SDK目录的tools/lib下,我们需要把它加入到我们
的Eclipse工程的build path下。
第二步,建立连接,获取设备
有了ddmlib,我们就可以使用里面的 AndroidDebugBridge 类来获取已经同步的设备的列表并建立连接
[java] view plain copy
- IDevicedevice;
- AndroidDebugBridgebridge=AndroidDebugBridge.createBridge();
- waitDeviceList(bridge);
- IDevicedevices[]=bridge.getDevices();
- device=devices[0];
上面的代码用到了一个waitDeviceList(bridge),主要是为了多次尝试连接,代码如下
[java] view plain copy
- privatestaticvoidwaitDeviceList(AndroidDebugBridgebridge){
- intcount=0;
- while(bridge.hasInitialDeviceList()==false){
- try{
- Thread.sleep(100);//如果没有获得设备列表,则等待
- ount++;
- }catch(InterruptedExceptione){}
- if(count>300){//设定时间超过300×100ms的时候为连接超时
- System.err.print("Timeout");
- break;
- }
- }
- }
这样我们就可以获得一个设备的类,IDevice,其中有一个getScreenshot()方法获得屏幕截图,类型为RawImage
[java] view plain copy
- RawImagerawScreen=device.getScreenshot();
后面的方法就和Android无关了,纯粹的转换,Rawimage转换到bufferedimage,再保存
[java] view plain copy
- if(rawScreen!=null){
- BufferedImageimage=null;
- intwidth2=landscape?rawScreen.height:rawScreen.width;
- intheight2=landscape?rawScreen.width:rawScreen.height;
- if(image==null){
- image=newBufferedImage(width2,height2,
- BufferedImage.TYPE_INT_RGB);
- }else{
- if(image.getHeight()!=height2||image.getWidth()!=width2){
- image=newBufferedImage(width2,height2,
- BufferedImage.TYPE_INT_RGB);
- }
- }
- intindex=0;
- intindexInc=rawScreen.bpp>>3;
- for(inty=0;y<rawScreen.height;y++){
- for(intx=0;x<rawScreen.width;x++,index+=indexInc){
- intvalue=rawScreen.getARGB(index);
- if(landscape)
- image.setRGB(y,rawScreen.width-x-1,value);
- else
- image.setRGB(x,y,value);
- }
- }
- ImageIO.write((RenderedImage)image,"PNG",newFile("D:/temp.jpg"));
- }
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- PopupWindow位置
- android 点击桌面图标,打开手机浏览器进入相应的网站
- android seekbar使用
- Android(安卓)摄像头自动对焦的几点注意
- Android(安卓)- SharedPreferences 数据存储
- Android常用工具库
- Android(安卓)Studio 设置内存大小及原理
- AlertDialog使用自定义的布局