Android捕获从surfaceview返回黑色图像的屏幕截图

时间:2015-04-17 11:49:26

标签: android screenshot surfaceview

任何人都可以给我完整的代码来完成这项操作。

我正在制作一款Android应用,我在其中使用surfaceview来显示相机。在按钮上单击我想捕获surfaceview的某些部分作为屏幕截图。当我尝试它时,它会返回黑色图像。某处我发现它是OpenGL的概念,但我对此并不确定。谁能告诉我如何编写捕获surfaceview的某些部分的解决方案?

以下是我使用的代码:

public void capture(View v){

File direct = new File(Environment.getExternalStorageDirectory()
            + "/MyFolder");

if (!direct.exists()) {
        direct.mkdir();
    }
    long time = System.currentTimeMillis();

    Bitmap bitmap;
    ImageView ll = (ImageView) findViewById(R.id.capture_screen);


    ll.setDrawingCacheEnabled(true);
    ll.buildDrawingCache(true);
    bitmap = Bitmap.createBitmap(ll.getDrawingCache());
    ll.setDrawingCacheEnabled(false);

    OutputStream fout = null;

    try {
        fout = new FileOutputStream(direct.getAbsolutePath() + "/" + time
                + ".jpg");
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();
        Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG)
                .show();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是xml文件代码,

<RelativeLayout
    android:id="@+id/upper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:theme="@style/Theme.Transparent"
        android:weightSum="4" >

        <RelativeLayout
            android:id="@+id/newer"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#90ffffff" >
        </RelativeLayout>

        <ImageView
            android:id="@+id/capture_screen"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:background="@drawable/squ" >
        </ImageView>
    </LinearLayout>
</RelativeLayout>

<LinearLayout
    android:id="@+id/lower"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fe6454"
    android:orientation="horizontal" >


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="capture"
        android:text="Capture" />


</LinearLayout>

0 个答案:

没有答案