How to make a Camera application using Android Studio 3.0.1

This program helps how to capture photos using an existing camera application.

There are two tools- Button, ImageView

Only one ImageView is used. When you click, You can save only one picture in ImageView.

You can add some more ImageView. Here is Program—

 

activity_main.xml—

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.camerademo.MainActivity">

    <Button
        android:onClick="camera"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Take a Photo" >
        </Button>

    <ImageView
        android:background="#FF00"
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button"
        android:layout_alignParentTop="true">


</ImageView>


</RelativeLayout>

MainActivity.java—

public class MainActivity extends AppCompatActivity {
    private static final int CAMERA_REQEST=1888;
    private ImageView img;
    private Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img=(ImageView)findViewById(R.id.img);

    }
    //***************************************************************
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
       try
        {
            boolean debug=false;
            if(requestCode!=CAMERA_REQEST)
                return;
            if(data==null)
                return;
            if(data.hasExtra("data"))
            {
                Bitmap thumbnail=data.getParcelableExtra("data");
                img.setImageBitmap(thumbnail);
                return;
            }

            int width=300;//img.getWidth();
            int height=300;//img.getHeight();
            setTitle(width + "," + height);
            if(debug)
                return;
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inJustDecodeBounds=true;
            BitmapFactory.decodeFile(uri.getPath(),options);
            int imgWidth=options.outWidth;
            int imgHeight=options.outHeight;
            if(width<=0)
                width=1;
            if(height<=0)
                height=1;
            int scaleFactor=Math.min(imgWidth/width,imgHeight/height);
            options.inJustDecodeBounds=false;
            options.inSampleSize=scaleFactor;
            options.inPurgeable=true;
            Bitmap bitmap=BitmapFactory.decodeFile(uri.getPath(),options);
            img.setImageBitmap(bitmap);

        }
        catch (Exception ex)
        {
            System.out.println(ex);

        }
    }
    //***************************************************************
    public void camera(View view)
    {
        try
        {
            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,CAMERA_REQEST);
            File pictureDirectory=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            String pictureName=getPictureName();
            File imagefile=new File(pictureDirectory, pictureName);
            Uri pictureUri=Uri.fromFile(imagefile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);

        }
        catch (Exception ex)
        {
            System.out.println(ex);
        }
    }
    private String getPictureName(){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp= sdf.format(new Date());
       return "Plane place image" + timestamp +".jpg";
    }
}

For giving permission in manifests—

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



Leave a Reply