如何在android中截取自定义相机?

时间:2016-06-13 15:29:10

标签: android android-layout android-studio android-camera android-custom-view

我将相机加载到framelayout中,我想截取屏幕截图。相机预览。

截取屏幕截图:

public void onClick(View v) {
    View v1 = L1.getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bm = v1.getDrawingCache();
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
    image = (ImageView) findViewById(R.id.ImageView011);
    image.setBackgroundDrawable(bitmapDrawable);

    //Bitmap bitmap = takeScreenshot();
    saveBitmap(bm);
}

保存屏幕截图:

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

保存图像时,除相机预览外,所有内容都在屏幕截图中 enter image description here

相机在框架布局中加载:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mobapptut.com.camapp.bellowLollipop"
android:baselineAligned="false"
android:id="@+id/containerImg">


<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false">


</FrameLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/captured_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="desc" />


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/plusBtnImg"
android:layout_gravity="left|bottom"
android:src="@mipmap/zoom_in2"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/imageView2"
android:background="#00ffffff" />


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minusBtnImg"
android:layout_gravity="left|bottom"
android:src="@mipmap/zoom_out2"
android:layout_above="@+id/plusBtnImg"
android:layout_toRightOf="@+id/imageView2"
android:background="#00ffffff" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back!"
android:id="@+id/backButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right|bottom"
android:layout_alignParentRight="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Picture"
android:id="@+id/button12"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take Screenshot"
android:id="@+id/Button01"
/>

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take Screenshot"
android:id="@+id/ImageView011"
/>
</RelativeLayout>

我做错了什么?在此先感谢:)

1 个答案:

答案 0 :(得分:2)

只需将您想要的视图作为参数传递给下面的方法

    public Bitmap takeScreenShot(View view) {
    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    view.buildDrawingCache();
    if (view.getDrawingCache() == null)
        return null;
    Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    view.destroyDrawingCache();
    return snapshot;
}

根据您的需要保存此位图。

相关问题