繁体   English   中英

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

[英]Rotate an horizontal saved png into vertical [Android]

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

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

代码:此函数从包含所有图像字节数据的数组生成 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. 我们可以旋转保存的图像而不是在保存时旋转吗? 我的图像在横向视图中完美保存。只想将它们旋转到 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); 

这奏效了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM