最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)矩形區(qū)域截屏的方法

 更新時(shí)間:2017年01月06日 12:00:25   作者:l465659833  
對(duì)屏幕進(jìn)行截屏并裁剪有兩種方式:早截圖和晚截圖,對(duì)于早截圖和晚截圖的概念大家通過(guò)本文詳解學(xué)習(xí)。本文重點(diǎn)給大家介紹android實(shí)現(xiàn)矩形區(qū)域截屏的方法,需要的朋友參考下

對(duì)屏幕進(jìn)行截屏并裁剪有兩種方式:早截圖和晚截圖。早截圖,就是先截取全屏,再讓用戶(hù)對(duì)截取到的圖片進(jìn)行修改;與之相對(duì)的,晚截圖,就是先讓用戶(hù)在屏幕上劃好區(qū)域,再進(jìn)行截圖和裁剪。其實(shí)兩者并沒(méi)有什么太大的區(qū)別,這篇就說(shuō)說(shuō)怎么實(shí)現(xiàn)晚截圖。

晚截圖可以分成三步:

1. 在屏幕上標(biāo)出截圖的矩形區(qū)域

2. 調(diào)用系統(tǒng)接口截屏

3. 對(duì)截圖進(jìn)行裁剪

效果圖如下:

矩形區(qū)域截屏

第一步、在屏幕上標(biāo)識(shí)出截圖區(qū)域

首先確定標(biāo)識(shí)截圖區(qū)域所需要的功能:

1. 手指拖動(dòng)形成矩形區(qū)域;

2. 可以拖動(dòng)已經(jīng)劃好的矩形區(qū)域進(jìn)行移動(dòng);

3. 可以拖動(dòng)矩形區(qū)域的邊框調(diào)整大??;

4. 選擇完成以后,有“確認(rèn)”和“取消”功能,“確認(rèn)”時(shí)可以獲得選取的區(qū)域位置。需要注意的是,按鈕的位置應(yīng)該能夠自適應(yīng),比如選框幾乎占據(jù)全屏的情況下,應(yīng)該把按鈕放到選框內(nèi)部。

最簡(jiǎn)單的方式就是寫(xiě)一個(gè)自定義View,根據(jù)touch的位置執(zhí)行不同的功能即可。實(shí)現(xiàn)很簡(jiǎn)單,只要細(xì)心把每一種狀態(tài)就行,代碼請(qǐng)看Bigbang項(xiàng)目的MarkSizeView類(lèi)。

第二步、調(diào)用系統(tǒng)接口截屏

截屏必須在Activity中進(jìn)行,因?yàn)樾枰{(diào)用startActivityForResult()。不過(guò)也可以把mMediaProjectionManager傳到service中進(jìn)行后續(xù)處理。

還要注意的是Activity本身在截屏的時(shí)候應(yīng)該是透明的,不能對(duì)要截取得內(nèi)容有影響。

直接看代碼:

public class ScreenCaptureActivity extends Activity {
 private static final String TAG = ScreenCaptureActivity.class.getName();
 private MediaProjectionManager mMediaProjectionManager;
 private int REQUEST_MEDIA_PROJECTION = 1;
 private SimpleDateFormat dateFormat;
 private String pathImage;
 private WindowManager mWindowManager;
 private ImageReader mImageReader;
 private MediaProjection mMediaProjection;
 private int mResultCode;
 private Intent mResultData;
 private VirtualDisplay mVirtualDisplay;
 private String strDate;
 private int windowWidth;
 private int windowHeight;
 private String nameImage;
 private int mScreenDensity;
 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 mMediaProjectionManager = (MediaProjectionManager) getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
 createVirtualEnvironment();
 startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == REQUEST_MEDIA_PROJECTION) {
  if (resultCode != Activity.RESULT_OK) {
  return;
  } else if (data != null && resultCode != 0) {
  mResultCode = resultCode;
  mResultData = data;
  startVirtual();
  new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
   @Override
   public void run() {
   startCapture();
   }
  },100);
  }
 }
 }
 @RequiresApi(api = Build.VERSION_CODES.KITKAT)
 private void createVirtualEnvironment() {
 dateFormat = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
 strDate = dateFormat.format(new Date());
 pathImage = Environment.getExternalStorageDirectory().getPath() + "/Pictures/";
 nameImage = pathImage + strDate + ".png";
 mMediaProjectionManager = (MediaProjectionManager) getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
 mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
 windowWidth = mWindowManager.getDefaultDisplay().getWidth();
 windowHeight = mWindowManager.getDefaultDisplay().getHeight();
 DisplayMetrics metrics = new DisplayMetrics();
 mWindowManager.getDefaultDisplay().getMetrics(metrics);
 mScreenDensity = metrics.densityDpi;
 mImageReader = ImageReader.newInstance(windowWidth, windowHeight, 0x1, 2); //ImageFormat.RGB_565
 Log.i(TAG, "prepared the virtual environment");
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public void startVirtual() {
 if (mMediaProjection != null) {
  Log.i(TAG, "want to display virtual");
  virtualDisplay();
 } else {
  Log.i(TAG, "start screen capture intent");
  Log.i(TAG, "want to build mediaprojection and display virtual");
  setUpMediaProjection();
  virtualDisplay();
 }
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public void setUpMediaProjection() {
 mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);
 Log.i(TAG, "mMediaProjection defined");
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 private void virtualDisplay() {
 mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
  windowWidth, windowHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
  mImageReader.getSurface(), null, null);
 Log.i(TAG, "virtual displayed");
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 private void startCapture() {
 strDate = dateFormat.format(new java.util.Date());
 nameImage = pathImage + strDate + ".png";
 Image image = mImageReader.acquireLatestImage();
 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);
 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
 image.close();
 Log.i(TAG, "image data captured");
 //保存截屏結(jié)果,如果要裁剪圖片,在這里處理bitmap
 if (bitmap != null) {
  try {
  File fileImage = new File(nameImage);
  if (!fileImage.exists()) {
   fileImage.createNewFile();
   Log.i(TAG, "image file created");
  }
  FileOutputStream out = new FileOutputStream(fileImage);
  if (out != null) {
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
   out.flush();
   out.close();
   Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
   Uri contentUri = Uri.fromFile(fileImage);
   media.setData(contentUri);
   this.sendBroadcast(media);
   Log.i(TAG, "screen image saved");
  }
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 private void tearDownMediaProjection() {
 if (mMediaProjection != null) {
  mMediaProjection.stop();
  mMediaProjection = null;
 }
 Log.i(TAG, "mMediaProjection undefined");
 }
}

第三步、對(duì)截圖進(jìn)行裁剪

根據(jù)第一步得到的截圖區(qū)域mRect對(duì)第二步中得到的截屏結(jié)果bitmap進(jìn)行裁剪:

if (mRect != null) {
 if (mRect.left < 0)
 mRect.left = 0;
 if (mRect.right < 0)
 mRect.right = 0;
 if (mRect.top < 0)
 mRect.top = 0;
 if (mRect.bottom < 0)
 mRect.bottom = 0;
 int cut_width = Math.abs(mRect.left - mRect.right);
 int cut_height = Math.abs(mRect.top - mRect.bottom);
 if (cut_width > 0 && cut_height > 0) {
 Bitmap cutBitmap = Bitmap.createBitmap(bitmap, mRect.left, mRect.top, cut_width, cut_height);
}

需要注意的是,在調(diào)用系統(tǒng)截屏功能的時(shí)候,如果手機(jī)有NavigationBar(虛擬導(dǎo)航欄),windowHeight的取值就是不包括NavigationBar的高度的,如果不進(jìn)行調(diào)整,就會(huì)導(dǎo)致截屏被壓縮。如何獲取屏幕的真實(shí)高度,可以參考Android如何判斷NavigationBar是否顯示(獲取屏幕真實(shí)的高度)。

而且NavigationBar還會(huì)導(dǎo)致截屏的結(jié)果出現(xiàn)邊框,邊框的顏色是透明的,原因是第二步代碼中的rowPadding!=0,截屏如下圖所示:

帶NavigationBar使用系統(tǒng)截圖的結(jié)果

那么如果我們想要對(duì)截圖的結(jié)果進(jìn)行保存或者裁剪,就必須要去除邊框,找出真正的內(nèi)容區(qū)域,也就是在第一個(gè)不透明的像素和最后一個(gè)不透明像素之間的內(nèi)容,然后才能對(duì)得到的區(qū)域進(jìn)行第三步的裁剪,代碼如下:

int[] pixel=new int[width];
bitmap.getPixels(pixel,0,width ,0,0,width,1);
int leftPadding=0;
int rightPadding=width;
for (int i=0;i<pixel.length;i++){
 if (pixel[i]!=0){
 leftPadding=i;
 break;
 }
}
for (int i=pixel.length-1;i>=0;i--){
 if (pixel[i]!=0){
 rightPadding=i;
 break;
 }
}
bitmap=Bitmap.createBitmap(bitmap,leftPadding, 0, rightPadding-leftPadding, height);

處理后的截圖如下:

取得截圖結(jié)果的內(nèi)容部分

你可能會(huì)覺(jué)得既然是rowPadding!=0導(dǎo)致出現(xiàn)邊框,而且邊框只在右邊,為什么不直接把右邊rowPadding寬度的內(nèi)容截掉呢?其實(shí)是因?yàn)槿绻徽{(diào)整windowHeight,就會(huì)在左邊也產(chǎn)生框,所以才用了上面的方法。

完整代碼可以參考Bigbang項(xiàng)目的MarkSizeView類(lèi)、ScreenCaptureActivity類(lèi)和ScreenCapture類(lèi)。

相關(guān)文章

  • SqlServer 巧妙解決多條件組合查詢(xún)

    SqlServer 巧妙解決多條件組合查詢(xún)

    開(kāi)發(fā)中經(jīng)常會(huì)遇得到需要多種條件組合查詢(xún)的情況,比如有三個(gè)表,年級(jí)表Grade(GradeId,GradeName),班級(jí)Class(ClassId,ClassName,GradeId),學(xué)員表Student(StuId,StuName,ClassId),現(xiàn)要求可以按年級(jí)Id、班級(jí)Id、學(xué)生名,這三個(gè)條件可以任意組合查詢(xún)學(xué)員信息
    2012-11-11
  • 何謂SQLSERVER參數(shù)嗅探問(wèn)題

    何謂SQLSERVER參數(shù)嗅探問(wèn)題

    這篇文章主要介紹了何謂SQLSERVER參數(shù)嗅探的相關(guān)問(wèn)題,本文通過(guò)數(shù)據(jù)庫(kù)的實(shí)際案例操作給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 查詢(xún)SQL Server數(shù)據(jù)庫(kù)服務(wù)器IP地址的多種有效方法

    查詢(xún)SQL Server數(shù)據(jù)庫(kù)服務(wù)器IP地址的多種有效方法

    作為數(shù)據(jù)庫(kù)管理員或開(kāi)發(fā)人員,了解如何查詢(xún)SQL Server數(shù)據(jù)庫(kù)服務(wù)器的IP地址是一項(xiàng)重要技能,本文將介紹幾種簡(jiǎn)單而有效的方法,幫助你輕松獲取這一信息,無(wú)論你是新手還是經(jīng)驗(yàn)豐富的專(zhuān)業(yè)人士,這些方法都能為你提供所需的信息,需要的朋友可以參考下
    2025-02-02
  • SQLServer2005創(chuàng)建定時(shí)作業(yè)任務(wù)

    SQLServer2005創(chuàng)建定時(shí)作業(yè)任務(wù)

    這篇文章主要為大家介紹了SQLServer2005創(chuàng)建定時(shí)作業(yè)任務(wù)的詳細(xì)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • SQL Transcation的一些總結(jié)分享

    SQL Transcation的一些總結(jié)分享

    相信大家對(duì)于SQL Transcation再熟悉不過(guò),它確保了數(shù)據(jù)庫(kù)的數(shù)據(jù)一致性和安全性,尤其在對(duì)數(shù)據(jù)執(zhí)行增刪時(shí),如果發(fā)生異常和錯(cuò)誤它就會(huì)觸發(fā)事務(wù)回滾,從而確保了我們數(shù)據(jù)的一致性和安全性,下面我們將通過(guò)分四部分介紹事件(Transcation)
    2012-08-08
  • SQL Server數(shù)據(jù)庫(kù)中批量導(dǎo)入數(shù)據(jù)的四種方法總結(jié)

    SQL Server數(shù)據(jù)庫(kù)中批量導(dǎo)入數(shù)據(jù)的四種方法總結(jié)

    數(shù)據(jù)導(dǎo)入一直是項(xiàng)目人員比較頭疼的問(wèn)題。其實(shí),在SQL Server中集成了很多成批導(dǎo)入數(shù)據(jù)的方法,接下來(lái)為大家介紹下常用的四種批量導(dǎo)入數(shù)據(jù)的方法,感興趣的各位可以參考下哈
    2013-03-03
  • 清除SQLServer日志的兩種方法

    清除SQLServer日志的兩種方法

    清除SQLServer日志的兩種方法...
    2007-01-01
  • SQLServe 重復(fù)行刪除方法

    SQLServe 重復(fù)行刪除方法

    刪除 SQL Server 表中的重復(fù)行,需要的朋友可以參考下。注意備份后再執(zhí)行如下操作。
    2009-11-11
  • 自動(dòng)定時(shí)備份sqlserver數(shù)據(jù)庫(kù)的方法

    自動(dòng)定時(shí)備份sqlserver數(shù)據(jù)庫(kù)的方法

    下面是我已經(jīng)證實(shí)可用的自動(dòng)備份的方法.需要的朋友可以參考下。
    2011-11-11
  • sql語(yǔ)句優(yōu)化之SQL Server(詳細(xì)整理)

    sql語(yǔ)句優(yōu)化之SQL Server(詳細(xì)整理)

    這篇文章主要介紹了sql語(yǔ)句優(yōu)化之SQL Server篇,整理的比較詳細(xì),推薦收藏
    2014-07-07

最新評(píng)論

屯留县| 彩票| 闵行区| 正宁县| 阿鲁科尔沁旗| 谷城县| 新田县| 柞水县| 同江市| 新安县| 余干县| 乐业县| 凤阳县| 鲁山县| 应城市| 武陟县| 左权县| 木里| 互助| 宁强县| 美姑县| 蓝山县| 郎溪县| 闵行区| 墨竹工卡县| 福清市| 米脂县| 湛江市| 德安县| 清水河县| 德令哈市| 夏河县| 临夏市| 昌邑市| 花莲县| 长宁区| 通海县| 绥江县| 启东市| 石台县| 剑阁县|