我正在开发一款应用,它使用前置摄像头为使用该应用的用户模拟镜像。我实施了一个"平视显示器"它位于cameraview前面并显示一些东西。这个目前工作正常。 现在我想实现一种方法来每5秒拍摄一次屏幕截图。
但如何拍摄截图? 我尝试了一种可能的解决方案,但这只是没有镜像的HuD截图,因为我必须将视图(v1)传递给此方法:
// create bitmap screen capture
Bitmap b1;
View v1 = (RelativeLayout) findViewById(R.id.RelLayout);
v1.setDrawingCacheEnabled(true);
b1 = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
//mPreviewLayout.setDrawingCacheEnabled(true);
//mPreviewLayout.buildDrawingCache();
//Bitmap b1 = mPreviewLayout.getDrawingCache();
String path = Environment.getExternalStorageDirectory().toString();
FileOutputStream out = null;
try {
File file = new File(path, "brushGuide" + instructionCounter + ".jpg");
out = new FileOutputStream(file);
b1.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Throwable ignore) {
}
}
我的布局和这个问题的重要代码非常少:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<FrameLayout
android:id="@+id/camPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</FrameLayout>
<ImageView android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="65dp"
></ImageView>
</RelativeLayout>
的onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// do we have a camera?
if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this,
"No camera feature on this device. App closing",
Toast.LENGTH_LONG).show();
}else{
mCameraId = findFirstFrontFacingCamera();
if (mCameraId >= 0) {
// Kamera wurde gefunden
mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
mPreviewLayout.removeAllViews();
startCameraInLayout(mPreviewLayout, mCameraId);
}
}
}
甚至可以只是拍摄设备实际内容的正常屏幕截图,而不需要植根设备,如果是的话:怎么样?
任何帮助将不胜感激。
答案 0 :(得分:3)
我实际上刚刚做了这个,获取整个Activity的屏幕截图很痛苦,同时还要确保从图像中删除StatusBar(因为它在绘图缓存中显示为黑色矩形) 。此方法还允许您使用某种颜色(即褪色)覆盖返回的图像:
public static Bitmap getActivitySnapshot(Activity activity, boolean fade){
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
Bitmap bmap = view.getDrawingCache();
Rect statusBar = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);
if(fade && snapshot != null){
Canvas canvas = new Canvas(snapshot);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#88121212"));
canvas.drawRect(0, 0, snapshot.getWidth(), snapshot.getHeight(), paint);
}
view.setDrawingCacheEnabled(false);
return snapshot;
}
如果你不想要褪色部分,那么重要的部分就是:
public static Bitmap getActivitySnapshot(Activity activity){
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
Bitmap bmap = view.getDrawingCache();
Rect statusBar = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);
view.setDrawingCacheEnabled(false);
return snapshot;
}
您可能还想捕获使用大型位图时可能抛出的OutOfMemoryError。
答案 1 :(得分:0)
此代码可以为您提供帮助。
/**
* @param v view to capture it may be any Layout
* @return
*/
public static Bitmap captureScreen(View v)
{
Bitmap bitmap = null;
try {
if(v!=null)
{
int width = v.getWidth();
int height = v.getHeight();
Bitmap screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
v.draw(new Canvas(screenshot));
}
} catch (Exception e)
{
Log.d("captureScreen", "Failed");
}
return bitmap;
}
答案 2 :(得分:0)
通过屏幕截图,您的Root视图副本会有帮助吗?
如果是这样,你可以使用:
public static Bitmap loadBitmapFromView(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getLayoutParams().width, view.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
view.draw(canvas);
return bitmap;
}
只需将根视图传递给此函数,即可保存位图。