Android custom camera SurfaceView OnClick save image on sdcard?

1.2k views Asked by At

Hi i want to implement custom android camera app which include imageview on top of the camera preview.After click the screen i want to store images in sdcard .So far i have done this part.But i can't click the SurfaceView and save image.Can anyone tell me how to resolve my problem.So far it open camera and it has imageview top of camera.But onclick method is not working.And also can anyone tell me how to save image when onclick function called. This is mycode.

This is mainlayout

<?xml version="1.0" encoding="utf-8"?>

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

    <SurfaceView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/surfaceView"
            android:layout_gravity="center"/>
</LinearLayout>

And this is custom view(activity_camera)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="bottom"
        >

    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/border"
            android:id="@+id/imageView"/>
</LinearLayout>

And this is java code.

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Sajith
 * Date: 11/18/14
 * Time: 3:41 PM
 * To change this template use File | Settings | File Templates.
 */
public class CameraPreview extends Activity implements SurfaceHolder.Callback,View.OnClickListener {
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.activity_camera, null);
        LayoutParams layoutParamsControl
                = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

    }



    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {

        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
    }
}

Thanks

1

There are 1 answers

1
Cri Rai On BEST ANSWER
capture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        camera.takePicture(null,null, jpegCallback);
    }
});

final Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(String.format("/sdcard/YourImage.jpg")); 
            outStream.write(data);
            outStream.close();
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        camera.startPreview();
    }
};