Open top menu






Example- How to Crop Capture image via Camera  in android ?


First you can Create view Layout.

1. main_activity.xml.
...................................................................................................................................................................
xml version="1.0" encoding="utf-8"?>

<LinearLayout  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"

android:orientation="vertical">

<Button

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text=" Camera "

  android:id="@+id/button"

  android:drawableBottom="@drawable/camra"

  android:layout_margin="20dp"

  />

 <ImageView

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:id="@+id/imageView" />

</LinearLayout>
...................................................................................................................................................................


2.Main_Activity.java.
...................................................................................................................................................................
public class CameraTakePic extends AppCompatActivity {
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final String IMAGE_DIRECTORY_NAME = "Camera";
    final int PIC_CROP = 2;
    GridView gridView;
    private UIHelper helper = new UIHelper(CameraTakePic.this);
    Context context = this;
    // directory name to store captured images and videos

   
Bitmap decodedByte;
    private Uri fileUri;
    int count = 0;
    ImageView img;
    ArrayList CamraTakeImgList = new ArrayList<>();

    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button takepic = (Button) findViewById(R.id.button);
        takepic.setOnClickListener(new View.OnClickListener() {
            @Override
           
public void onClick(View v) {
                captureImage();
            }
        });
        gridView = (GridView) findViewById(R.id.gridView);
          img=(ImageView)findViewById(R.id.imageView);
    }

    private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent,                  CAMERA_CAPTURE_IMAGE_REQUEST_CODE);   
 }

  public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    private static File getOutputMediaFile(int type) {
        // External sdcard location
       
File mediaStorageDir = new File(
        Environment.getExternalStoragePublicDirectory(Environment.
                   DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);
        // Create the storage directory if it does not exist
       
if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        
+ IMAGE_DIRECTORY_NAME + " directory");
                return null;  }
        }

   // Create a media file name
 
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss"
, Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                   
+ "IMG_" + timeStamp + ".BMP");
        } else {
            return null;
        }
        return mediaFile;
    }

    private void previewCapturedImage() {
        try {
            count++;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.
                                                                            getPath(),   options);
            ColorItem select = new ColorItem();
            select.setBitmap(bitmap);
            select.setColorName("Pic" + " " + count);
            CamraTakeImgList.add(select);
            SelectedImageAdapter seletimg =
           new SelectedImageAdapter(CameraTakePic. this, CamraTakeImgList);                       
            gridView.setAdapter(seletimg);
         } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

public void onActivityResult(int requestCode, int resultCode, Intent data)            {
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                //previewCapturedImage();
               
performCrop();
            }
        }else if(requestCode == PIC_CROP){
            Bundle extras = data.getExtras();
            //get the cropped bitmap
           
Bitmap thePic = extras.getParcelable("data");
            img.setImageBitmap(thePic);
        }
    }


     private void performCrop(){
        try {
            //call the standard crop action intent (the user device may not support it)
           
Intent cropIntent = new Intent("com.android.camera.action.CROP");
            //indicate image type and Uri
           
cropIntent.setDataAndType(fileUri, "image/*");
            //set crop properties
           
cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
           
cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
           
cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            //retrieve data on return
           
cropIntent.putExtra("return-data", true);
            //start the activity - we handle returning in onActivityResult
           
startActivityForResult(cropIntent, PIC_CROP);



        }
        catch(ActivityNotFoundException anfe){
            //display an error message
           
String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}
...................................................................................................................................................................







try this code

0 comments