2

Can anyone please give me the working whole code to do this operation.

I'm making an Android app in which I use surfaceview for showing camera. On button click I want to capture some part of surfaceview as screenshot. When I tried it, it returns me black image. Somewhere I found that it is a concept of OpenGL, but i'm not sure about this. Can anyone tell me how to write solution for capturing some part of surfaceview?

Here is the code I used:

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();
    }
}

Here is the xml file code,

<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>
Sahil Garg
  • 263
  • 1
  • 20

0 Answers0