如何在相同的分辨率下增加JPG文件大小?

时间:2014-12-27 15:20:10

标签: android android-camera filesize image-compression resize-crop

我可以捕捉,裁剪,调整大小和保存照片。以相同分辨率保存照片(133x171)。我需要这个分辨率和20KB jpeg文件大小。 有时它捕获小于20KB。我想保存所有超过20KB的照片。 JPEG质量:100

StartPreview代码

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方法:

@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方法:

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();
}

更新#1: 也许这不是最好的方式。但我试过了。将图片文件写入存储器后,使用此方法将一些EXIF数据添加到图像中。

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 个答案:

没有答案