1

我正在尝试使用相机,并且能够捕获并保存高质量的图像,但前提是相机处于横向模式。

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

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

现在,当我尝试以纵向模式捕获高质量图像时遇到一个问题:如果我尝试旋转捕获的图像,它似乎不起作用。

这是我的源代码:

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

0 回答 0