以编程方式屏幕屏幕的某些部分

问题描述:

在我的应用程序中,我有一个矩形ImageView,其中包含一个从中间切出一个洞的白色背景。在它后面我设置了相机预览,所以只有孔显示相机。我怎么会拍一张IMAGEVIEW.X AND Y AND HEIGHT AND WIDTH的照片,这样我仍然可以看到脸。基本上裁剪屏幕截图的大小imageview
enter image description here以编程方式屏幕屏幕的某些部分

希望这会有所帮助。

private void takeScreenshot() { 
Date now = new Date(); 
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 

try { 
    // image naming and path to include sd card appending name you choose for file 
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 

    // create bitmap screen capture 
    View v1 = getWindow().getDecorView().getRootView(); 
    v1.setDrawingCacheEnabled(true); 
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
    v1.setDrawingCacheEnabled(false); 

    File imageFile = new File(mPath); 

    FileOutputStream outputStream = new FileOutputStream(imageFile); 
    int quality = 100; 
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
    outputStream.flush(); 
    outputStream.close(); 

    openScreenshot(imageFile); 
} catch (Throwable e) { 
    // Several error may come out with file handling or OOM 
    e.printStackTrace(); 
} 
}