I am working on an app, which uses the frontcamera to simulate a mirror for the person using the app. I have implemented a "Head-up-Display" which is in front of the cameraview and displays some stuff. This works fine at the moment. Now I'd like to implement a method to take a screenshot every 5s.
But how to take a screenshot? I tried one possible solution, but this just takes a screenshot of the HuD without the mirror, because I have to pass a view (v1) to this method:
// 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) {
}
}
My layout and the important code for this question is quite less:
<?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);
}
}
}
Is even possible to just take a normal screenshot of the actual content of the devices display without rooting the device, and if yes: How?
Any help will be appreciated.