How to increase JPG file size in same resolution?

771 Views Asked by At

I can capturing, cropping, resizing and saving photos. Saving photos with same resolution (133x171). I need this resolution and greather than 20KB jpeg file size. Sometimes it capture smaller than 20KB. I want to save all photos greather than 20KB. JPEG Quality: 100

StartPreview Codes

public void surfaceCreated(SurfaceHolder holder) {      

    setWillNotDraw(false);

    Log.d("Variable", "surfaceCreated");

    try {
        mCamera.setPreviewDisplay(holder);

        Camera.Parameters parameters = mCamera.getParameters();

        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            parameters.set("orientation", "portrait");
            mCamera.setDisplayOrientation(90);
          }
        parameters.set("jpeg-quality", 100);       
        mCamera.setParameters(parameters);

        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("Method.surfaceCreated", "Error setting camera preview: " + e.getMessage());
    }
}

PictureTaken method:

@Override
   public void onPictureTaken(byte[] data, Camera camera) {
       // TODO Auto-generated method stub
       /*Bitmap bitmapPicture   = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */

       File pictureFile = getOutputMediaFile();
       if (pictureFile == null){
           Log.d("Method.PictureCall", "Error creating media file, check storage permissions: ");
           return;
       }

       data = cropImage(data);

       try {
           FileOutputStream fos = new FileOutputStream(pictureFile);
           fos.write(data);
           fos.close();

           Toast.makeText(c, "saved: " + pictureFile.toString(), Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            Log.d("Method.PictureCallBack", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("Method.PictureCallBack", "Error accessing file: " + e.getMessage());
        }

        camera.startPreview();
  }

cropImage Method:

public byte[] cropImage (byte[] data) {

    Point start_xy = getCaptureStartPoint();
    Point cropRes = getCaptureResolution();

    int stride = cropRes.x;

    int[] pixels = new int[cropRes.x * cropRes.y];

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

    bitmap = rotateBitmap(bitmap, 90);
    bitmap.getPixels(pixels, 0, stride, start_xy.x, start_xy.y, cropRes.x, cropRes.y);

    bitmap = Bitmap.createBitmap(pixels, 0, stride, cropRes.x, cropRes.y, Config.ARGB_8888);

    bitmap = Bitmap.createScaledBitmap(bitmap, efoto_x, efoto_y, false);

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);

    return bos.toByteArray();
}

UPDATE #1: Perhaps this isn't best way. But I tried. After write picture file to storage adding some EXIF data to image with this method.

private void addEXIFData(final File f) throws IOException {
    ExifInterface exif = new ExifInterface(f.getAbsolutePath());  
    exif.setAttribute(ExifInterface.TAG_MAKE, "Photo Photo Photo Photo........ bla bla blaaaaa");
    exif.setAttribute(ExifInterface.TAG_MODEL, "Photo Photo Photo Photo........ bla bla blaaaaa");
    exif.setAttribute(ExifInterface.TAG_ORIENTATION, "90");
    exif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, "150");      

    exif.saveAttributes();
}
0

There are 0 best solutions below