Created
February 15, 2018 15:41
-
-
Save adityajoshi12/8c5b7b571ba2792ff756a982b6b89ff3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TakePictureDemo extends Baseactivity { | |
protected View view; | |
protected ImageView imgViewCamera; | |
protected int LOAD_IMAGE_CAMERA = 0, CROP_IMAGE = 1, LOAD_IMAGE_GALLARY = 2; | |
private Uri picUri; | |
private File pic; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setReference(); | |
setToolbarElevation(7); | |
setToolbarSubTittle(this.getClass().getSimpleName()); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
} | |
@Override | |
public void setReference() { | |
view = LayoutInflater.from(this).inflate(R.layout.activity_takepicture_demo, container); | |
imgViewCamera = (ImageView) view.findViewById(R.id.img_camera); | |
} | |
public void takePicture_Click(View v) { | |
final CharSequence[] options = {"Take Photo", "Choose from Gallery"}; | |
AlertDialog.Builder builder = new AlertDialog.Builder(TakePictureDemo.this); | |
builder.setTitle("Select Pic Using..."); | |
builder.setItems(options, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int item) { | |
if (options[item].equals("Take Photo")) { | |
try { | |
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); | |
pic = new File(Environment.getExternalStorageDirectory(), | |
"tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); | |
picUri = Uri.fromFile(pic); | |
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, picUri); | |
cameraIntent.putExtra("return-data", true); | |
startActivityForResult(cameraIntent, LOAD_IMAGE_CAMERA); | |
} catch (ActivityNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} else if (options[item].equals("Choose from Gallery")) { | |
Intent intent = new Intent(Intent.ACTION_PICK, | |
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(Intent.createChooser(intent, "Select Picture"), LOAD_IMAGE_GALLARY); | |
} | |
} | |
}); | |
builder.show(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
//super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == LOAD_IMAGE_CAMERA && resultCode == RESULT_OK) { | |
CropImage(); | |
} | |
else if (requestCode == LOAD_IMAGE_GALLARY) { | |
if (data != null) { | |
picUri = data.getData(); | |
CropImage(); | |
} | |
} | |
else if (requestCode == CROP_IMAGE) { | |
if (data != null) { | |
// get the returned data | |
Bundle extras = data.getExtras(); | |
// get the cropped bitmap | |
Bitmap photo = extras.getParcelable("data"); | |
imgViewCamera.setImageBitmap(photo); | |
if (pic != null) | |
{ | |
// To delete original image taken by camera | |
if (pic.delete()) | |
Common.showToast(TakePictureDemo.this,"original image deleted..."); | |
} | |
} | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == android.R.id.home) { | |
finish(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
protected void CropImage() { | |
try { | |
Intent intent = new Intent("com.android.camera.action.CROP"); | |
intent.setDataAndType(picUri, "image/*"); | |
intent.putExtra("crop", "true"); | |
intent.putExtra("outputX", 200); | |
intent.putExtra("outputY", 200); | |
intent.putExtra("aspectX", 3); | |
intent.putExtra("aspectY", 4); | |
intent.putExtra("scaleUpIfNeeded", true); | |
intent.putExtra("return-data", true); | |
startActivityForResult(intent, CROP_IMAGE); | |
} catch (ActivityNotFoundException e) { | |
Common.showToast(this, "Your device doesn't support the crop action!"); | |
} | |
} | |
public Bitmap CompressResizeImage(Bitmap bm) | |
{ | |
int bmWidth = bm.getWidth(); | |
int bmHeight = bm.getHeight(); | |
int ivWidth = imgViewCamera.getWidth(); | |
int ivHeight = imgViewCamera.getHeight(); | |
int new_height = (int) Math.floor((double) bmHeight *( (double) ivWidth / (double) bmWidth)); | |
Bitmap newbitMap = Bitmap.createScaledBitmap(bm, ivWidth, new_height, true); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
newbitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos); | |
byte[] b = baos.toByteArray(); | |
Bitmap bm1 = BitmapFactory.decodeByteArray(b, 0, b.length); | |
return bm1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment