0

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

开始预览代码

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

拍照方法:

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

裁剪图像方法:

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

0 回答 0