Friday, 5 August 2016

Capture and set image


Capturing image fom camera

static final int REQUEST_IMAGE_CAPTURE = 1;

Uri photoURI;

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    ++i;
    String fname = "profile" + i + ".png";
    File file = new File(myDir, fname);
    file.deleteOnExit();
    photoURI = Uri.fromFile(file);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) 
!= null) {
  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
  startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
  }
}

@Overridepublic void onActivityResult(int requestCode, int resultCode, 
Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity()
.RESULT_OK) {
     imagepath = getRealPathFromURI(photoURI);
     imagepath = setOreintation(imagepath);
     Picasso.with(getActivity())
           .load(new File(imagepath)).transform(new CircleTransform())
             .into(imgProfile);

        uploadProfileImage(imagepath);
   }
}
Add a class for circle shape image
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.squareup.picasso.Transformation;

public class CircleTransform implements Transformation {
    @Override    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    }

    @Override    public String key() {
        return "circle";
    }
}

No comments:

Post a Comment