Skip to content

Instantly share code, notes, and snippets.

@oak-tree
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save oak-tree/872010e8d29e59e9cb42 to your computer and use it in GitHub Desktop.

Select an option

Save oak-tree/872010e8d29e59e9cb42 to your computer and use it in GitHub Desktop.
Android crop image - psudo code
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] data, Camera arg1) {
// TODO Auto-generated method stub
try {
private final INT_QUALITY = 2;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = INT_QUALITY;
bitmapPicture = BitmapFactory.decodeByteArray(data, data.length,options);
int dim = Math.min(bitmapPicture.getHeight(),bitmapPicture.getWidth())
bitmapPicture = Bitmap.createBitmap(
bitmapPicture,
0,
0,
dim,
dim
);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();
//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
} catch (IOException e) {
// do stuff
} finally {
if (fos != null) {
fos.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment