0

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();
}
RedLEON
  • 281
  • 2
  • 7
  • 19
  • As a general method, you can add some null bytes (binary 00000000) at the end of the file. – kiwixz Dec 27 '14 at 15:42
  • Thanks @iKiWiXz good advice. I tried saving my image with different software. 20KB -> 16KB (MS Paint). 20KB -> 22KB (Photoshop CS5).. Maybe I can write EXIF data for additional size. – RedLEON Dec 27 '14 at 15:52
  • updated @iKiWiXz Could you check it please. What do you say about it? – RedLEON Jan 20 '15 at 22:33
  • I was really thinking about null bytes, not EXIF data. You can do it manually (for instance) with an hexadecimal editor. It isn't specific to images. – kiwixz Jan 21 '15 at 11:37
  • I see. But I need to increase it with code. Not external tools. And I thing it has done with EXIF data. @iKiWiXz – RedLEON Jan 24 '15 at 19:36
  • What about [OutputStreamWriter](http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html) ?? – kiwixz Jan 25 '15 at 00:30

0 Answers0