0

When i take a picture from the camera i get an image of size 300-500 K.B (1800 X 1700 px).

But when i take a picture from my application i get an image of size 30-50 K.B (150 X 200 px).

How can i resize 150 x 200 px to something like 300 x 400 px??

because the server i am sending this image to, won't accept images lesser than 280 x 360 px dimensions.

The code i am using now is:

Bitmap bit = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] ba = bao.toByteArray();
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My - Images");
File f = new File(imagesFolder, "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(ba);
fo.flush();
fo.close();

i am not compressing the image, but still i get 10 times lesser image size compared to the image take using the camera from home screen.

Thank You

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

3 Answers3

1

You need to create new bitmap object and use that using Bitmap.createScaledBitmap()

Bitmap bit = (Bitmap) data.getExtras().get("data");
Bitmap bitmap =Bitmap.createScaledBitmap(bit, newWidth, newHeight, filter);
// use bitmap instead of bit object
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • yeah. i ended up using this code. But can you please tell me why i am getting such a low size image compared to the image taken from camera?? – Archie.bpgc Sep 26 '12 at 07:44
  • try with this way without creating scaled bitmap bit.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("f"))); – Pratik Sep 26 '12 at 07:55
1

To get full sized camera image you should point camera to save picture in some temporary file. then only you can get a actual size image.

intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo;
try
{
    // place where to store camera taken picture
    photo = this.createTemporaryFile("picture", ".jpg");
    photo.delete();
}
catch(Exception e)
{
    Log.v(TAG, "Can't create file to take picture!");
    Toast.makeText(activity, "Please check SD card! Image shot is impossible!", 10000);
    return false;
}
mImageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
//start camera intent
activity.startActivityForResult(this, intent, MenuShootImage);

private File createTemporaryFile(String part, String ext) throws Exception
{
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/";
if(!tempDir.exists())
{
    tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}

Then after image capture intent finished to work - just grab your picture from imageUri using following code:

public void grabImage(ImageView imageView)
{
this.getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
try
{
    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
    imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "Failed to load", e);
}
}


//called after camera intent finished
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(requestCode==MenuShootImage && resultCode==RESULT_OK)
{
   ImageView imageView;
   //... some code to inflate/create/find appropriate ImageView to place grabbed image
   this.grabImage(imageView);
}
super.onActivityResult(requestCode, resultCode, intent);
}
Pie
  • 262
  • 2
  • 10
  • i am already saving it on the SD card. should i definitely save it as a temp file? right now its saved permanently – Archie.bpgc Sep 26 '12 at 07:43
0

Before you take the picture you should configure the camera.

first

   camera.open();

check the supported picture sizes:

   Camera.Parameters p = camera.getParameters();
   List<Size> ss = p.getSupportedPictureSizes();
   Iterator<Size> it = ss.iterator();
   Size sizeIWant = null;
   while(it.hasNext()){
   Size s = it.next();
       <compute sizeIWant based on various sizes provided>
   }
   p.setPictureSize(sizeIWant.width, sizeIWant.height);
   camera.setParameters(p);

then take the pictures (your code), don't forget to call camera.release().

ALiGOTec
  • 347
  • 2
  • 15