通过相机调整和旋转高质量捕获的图像

时间:2014-04-09 23:20:11

标签: android android-camera image-rotation resize-image high-resolution

我尝试使用相机,我能够拍摄并保存高质量的图像,但仅限于相机处于横向模式时。

我还能够以横向和纵向模式捕捉和保存低质量的图像。

我使用矩阵对象旋转以纵向模式拍摄的照片,并保存得很好。

当我尝试在纵向模式下捕获高质量图像时,我遇到了问题:如果我尝试旋转捕获的图像,它似乎无法正常工作。

这是我的源代码:

public void onPictureTaken(byte[] arg0, Camera arg1) {

  // Test if the devicxe is in Portrait mode 

  if (FullscreenActivity.this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

  // Create a Bitmap from byte []
    Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
        arg0.length);

    // Create a Matrix, rotate the image and create a new one Bitmap
    Matrix rotateMatrix = new Matrix();
    rotateMatrix.postRotate(90);
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapPicture, 0, 0,
        bitmapPicture.getWidth(), bitmapPicture.getHeight(),
        rotateMatrix, false);

    // resize bitmap
    Bitmap resized = Bitmap.createScaledBitmap(rotatedBitmap, 1024, 768, true);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    resized.compress(CompressFormat.JPEG, 100, bos);

    // convert new compressed bitmap in a byte []
    byte[] array = bos.toByteArray();

    Uri uriTarget = getContentResolver().insert(
        Media.EXTERNAL_CONTENT_URI, new ContentValues());

    OutputStream imageFileOS;
    try {
      imageFileOS = getContentResolver().openOutputStream(
          uriTarget);
      imageFileOS.write(array);
      imageFileOS.flush();
      imageFileOS.close();
      Toast.makeText(FullscreenActivity.this,
          "Image saved: " + uriTarget.toString(),
          Toast.LENGTH_LONG).show();

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

0 个答案:

没有答案
相关问题