Wednesday, 10 February 2016

Camera Capture Simple Application

Steps : 1

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<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"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context="com.example.suraniujas.simplescan.MainActivity">

    <RelativeLayout         
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:id="@+id/rele1">

        <Button 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/btn_scan"
              android:text="Scan"
              android:layout_centerHorizontal="true"/>

        <FrameLayout 
              android:layout_width="350dp" 
              android:layout_height="350dp"
              android:id="@+id/camera_preview"
              android:layout_centerHorizontal="true" 
              android:layout_below="@+id/btn_scan">

        </FrameLayout>

        <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Capture"
             android:id="@+id/btn_capture"
             android:layout_centerHorizontal="true"
             android:layout_below="@+id/camera_preview"
            />
        

    </RelativeLayout>
</RelativeLayout>



Steps : 2
Than After MainActivity.java 
 
 
 
 package com.example.suraniujas.simplescan;


import android.hardware.Camera;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    Button scan;
    private FrameLayout scanFrame;
    private Camera mCamera = null;
    private CameraView mCameraView = null;
    public int backId=1;

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

        scan = (Button)findViewById(R.id.btn_scan);
        final Button btnCapture = (Button) findViewById(R.id.btn_capture);
        btnCapture.setEnabled(false);
        scan.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                try {
                    mCamera = Camera.open();


                } catch (Exception e) {
                    Log.d("ERROR", "Failed to get camera: " + e.getMessage());
                }

                if (mCamera != null) {
                    mCameraView = new CameraView(getBaseContext(), mCamera);
                    scanFrame = (FrameLayout) findViewById(R.id.camera_preview);
                    scanFrame.addView(mCameraView);


                }
                btnCapture.setEnabled(true);


            }
        });
        btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                mCamera.stopPreview();
                btnCapture.setVisibility(View.INVISIBLE);
            }
        });

    }
}
 
 
 
Steps : 3  
 
Than After Create One Java Class CameraView.java 


package com.example.suraniujas.simplescan;

import android.content.Context;


import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.IOException;

/** * Created by SURANI UJAS on 09-02-2016. */
 public class CameraView extends SurfaceView  implements SurfaceHolder.Callback {


        private SurfaceHolder mHolder;
        private Camera mCamera;

        public CameraView(Context context, Camera camera) {
                super(context);
                mCamera = camera;
                mCamera.setDisplayOrientation(90);
                mHolder = getHolder();
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        }



        @Override        public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                        mCamera.setPreviewDisplay(surfaceHolder);
                        mCamera.startPreview();
                } catch (Exception e) {
                        Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
                }
        }

        @Override 
 public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
                if (mHolder.getSurface() == null)
                        return;
                try {
                        mCamera.stopPreview();
                } catch (Exception e) {

                }

                try {
                        mCamera.setPreviewDisplay(mHolder);
                        mCamera.startPreview();

                } catch (Exception e) {
                        Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
                }
        }


        @Override 
 public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                mCamera.stopPreview();
                mCamera.release();
        }
}


Steps : 4

Than After AndroidManifest.xml

To Permission Camera 

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
 
 
 
Steps : 5
 
 Run The Project