Umesh
Umesh

Reputation: 86

Get picture from camera

Get picture from camera when object(any document)fully occupies the camera view inside the camera boundary. actually i have created custom camera and i want to get any object picture when picture fully come inside camera boundary.I have set the camera boundary inside the xml part. This is my code please check and correct me fine out this problem.

public class MainActivity extends ActionBarActivity {

    private ImageSurfaceView mImageSurfaceView;
    private Camera camera;
    public static FrameLayout cameraPreviewLayout;
    private ImageView capturedImageHolder;
    String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        cameraPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview);
        camera = checkDeviceCamera();
        mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
        cameraPreviewLayout.addView(mImageSurfaceView);




        Button captureButton = (Button) findViewById(R.id.button);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                camera.takePicture(null, null, pictureCallback);


            }
        });
    }

    private Camera checkDeviceCamera() {
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mCamera;
    }

    PictureCallback pictureCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();;
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }
    };


    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");

        Log.e("mediaFile",""+mediaFile.length());

        return mediaFile;
    }


}

This is the xml file.

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="400dp"
        android:layout_height="320dp"
        android:background="@drawable/camera_backround"
        android:padding="10dp"
        android:layout_margin="10dp"
        >

    </FrameLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/capture_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
      />



</RelativeLayout>

This is my SurfaceView class.

public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Camera.PictureCallback, Camera.PreviewCallback {

    private Camera camera;
    private SurfaceHolder surfaceHolder;
    Context context;
    private boolean mFocused;
    private boolean imageProcessorBusy=true;
    private int width;
    private int height;
    public void setImageProcessorBusy(boolean imageProcessorBusy) {
        this.imageProcessorBusy = imageProcessorBusy;
    }


    public ImageSurfaceView(Context context, Camera camera) {
        super(context);
        this.camera = camera;
        this.surfaceHolder = getHolder();
        this.surfaceHolder.addCallback(this);
        this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.context=context;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera.setDisplayOrientation(90);
        try {
            this.camera.setPreviewDisplay(holder);
            this.camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        refreshCamera();
        Log.e("camera", "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

            Log.e("camera","surfaceDestroyed");
            this.camera.stopPreview();
            this.camera.setPreviewCallback(null);
            this.camera.release();
            this.camera = null;

    }

    private void refreshCamera() {
        try {
            Log.e("camera", "refreshCamera");
            camera.stopPreview();
        } catch (Exception e) {
        }
        try {
            Log.e("camera", "refreshCamera----2");
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            camera.setPreviewCallback(this);
        } catch (Exception e) {


        }
    }


    @Override
    public void onPictureTaken(byte[] bytes, Camera camera) {
        android.hardware.Camera.Size pictureSize = camera.getParameters().getPictureSize();

        Log.d("Check", "onPictureTaken - received image " + pictureSize.width + "x" + pictureSize.height);
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

        android.hardware.Camera.Size pictureSize = camera.getParameters().getPreviewSize();
        // layout in the activity that the cameraView will placed in
        int layoutWidth = MainActivity.cameraPreviewLayout.getWidth();
        int layoutHeight = MainActivity.cameraPreviewLayout.getHeight();
        Log.e("layoutWidth","------"+layoutWidth);
        Log.e("layoutHeight", "---------" + layoutHeight);
        Log.d("onPreviewFrame", "onPreviewFrame - received image " + pictureSize.width + "x" + pictureSize.height
                + " focused: "+ mFocused +" imageprocessor: "+(imageProcessorBusy?"busy":"available"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = width;
        setMeasuredDimension(width, width);

    }


}

Upvotes: 0

Views: 464

Answers (1)

SaravInfern
SaravInfern

Reputation: 3388

you can use intent to capture the image data from camera, try this

private static final int TAKE_PICTURE = 1000;    
private Uri imageUri;

In your button click

Button captureButton = (Button) findViewById(R.id.button);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                takePhoto();   

            }
        });

public void takePhoto() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }
}

Your xml code

<ImageView
    android:id="@+id/Imageview"
    android:layout_width="400dp"
    android:layout_height="320dp"
    android:background="@drawable/camera_backround"
    android:padding="10dp"
    android:layout_margin="10dp"
    />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capture_button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
  />

Note: if you are going to take multiple pictures append timestamp to the filename.

Upvotes: 1

Related Questions