将水平保存的 png 旋转为垂直 [Android]

Rotate an horizontal saved png into vertical [Android]

我有一个应用程序可以在肖像模式下点击图像并将它们保存在带有 exyension png 的 SDCARD 中。我在图像上使用了额外的叠加层,然后保存 it.My 图像以横向模式保存。

我试过使用矩阵,但无法得到正确的解决方案。

代码:此函数从包含所有图像字节数据的数组生成 png。

          public void generatePng(byte[][] global) {


              for (int i = 1; i < 25; i++) {
                 // Matrix matrix = new Matrix();
                 // matrix.postRotate();
                  // createa matrix for the manipulation


                  Bitmap cameraBitmap = BitmapFactory.decodeByteArray(global[i], 0, global[i].length);
                  int wid = cameraBitmap.getWidth();
                  int hgt = cameraBitmap.getHeight();

                  Matrix matrix = new Matrix();
                  // resize the bit map
                  // matrix.postScale(scaleWidth, scaleHeight);
                  // rotate the Bitmap
                  matrix.postRotate(45);

                  Bitmap newImage = Bitmap.createBitmap
                          (cameraBitmap,0,0,wid, hgt, matrix,true);

                  Canvas canvas = new Canvas(newImage);

                  canvas.drawBitmap(cameraBitmap, 0f, 0f, null);

                  Drawable drawable = getResources().getDrawable(R.drawable.overlay11);
                 // drawable.
                //  drawable.setBounds(40, 40, drawable.getIntrinsicWidth() + 40, drawable.getIntrinsicHeight() + 40);
                  drawable.setBounds(0, 0, wid, hgt);

                  drawable.draw(canvas);


                  File mediaStorageDir = new File("/sdcard/", "pics");

                  File myImage = new File(mediaStorageDir.getPath() + File.separator + "pic" + glo + ".png");
                  Log.d("naval", "File path :" + myImage);
                  glo++;

                  try {
                      FileOutputStream out = new FileOutputStream(myImage);
                      newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


                      out.flush();
                      out.close();
                  } catch (FileNotFoundException e) {
                      Log.d("In Saving File", e + "");
                  } catch (IOException e) {
                      Log.d("In Saving File", e + "");
                  }
              }
              counter =0;
             // camera.startPreview();


          }

我认为我使用的覆盖会产生问题。 有 2 个问题

  1. 我们可以旋转保存的图像而不是在保存时旋转吗?我的图像在风景中完美保存 view.Just 想将它们旋转为 Potrait。
  2. 我正在尝试从所有这些图像创建一个 gif。我们可以旋转 gif 吗?

我建议首先使用 ExifInterface 检查 Bitmap 的方向。

ExifInterface exif = new ExifInterface(file.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

然后用Matrix旋转Bitmap:

Matrix matrix = new Matrix();

if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}

myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true);

然后你终于可以将旋转后的 Bitmap 保存在 sdcard 中了。确保将按比例缩小的位图加载到内存中以避免 java.lang.OutOfMemory 异常。

matrix.postRotate(270); 

这有效。