我有一个应用程序,该应用程序可以在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个问题


我们可以旋转保存的图像而不是保存时吗?我的图像可以完美地保存在风景视图中。只需要将它们旋转到Potrait。
我正在尝试从所有这些图像创建一个gif。我们可以旋转gif吗?

最佳答案

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

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异常。

07-28 03:29
查看更多