用于旋转相机意图捕获的图像的代码在 Android 中不起作用

2024-02-14

我有一个问题,使用相机意图捕获的图像被旋转,我在为什么使用相机意图捕获的图像在某些 Android 设备上会旋转? https://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android

因此,根据那里的建议答案,我现在尝试旋转使用相机获得的图像(我已使用相机意图)。我的代码是,

public class SimpleCameraActivity extends Activity {
private static final int TAKE_PICTURE = 1888;
private Uri imageUri;
ImageView imageView;
Bitmap bitmap;
Button btnOK, btnDiscard;
RelativeLayout rLButtons;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_camera);
    imageView = (ImageView) findViewById(R.id.image_view_sc);
    captureImage();
}

public void captureImage() {
    Intent intentCamera = new Intent("android.media.action.IMAGE_CAPTURE");
    File filePhoto = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
    imageUri = Uri.fromFile(filePhoto);
    MyApplicationGlobal.imageUri = imageUri.getPath();
    intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intentCamera, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentFromCamera) {
    super.onActivityResult(requestCode, resultCode, intentFromCamera);
    if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
        if (intentFromCamera != null) {
            Bundle extras = intentFromCamera.getExtras();
            if (extras.containsKey("data")) {
                bitmap = (Bitmap) extras.get("data");
            } else {
                bitmap = getBitmapFromUri();
            }
        } else {
            bitmap = getBitmapFromUri();
        }
        checkForRotation();
        //rotateImage();
        //rotateImage1();
        rotateImage3();
        //imageView.setImageBitmap(bitmap);
        // imageView.setImageURI(imageUri);
    } else {
        finish();
    }
}

public void rotateImage() {
    Bitmap targetBitmap = Bitmap.createBitmap(100, 80, bitmap.getConfig());
    Canvas canvas = new Canvas(targetBitmap);
    Matrix matrix = new Matrix();
    matrix.setRotate(180, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    matrix.setRotate(90);
    canvas.drawBitmap(bitmap, matrix, new Paint());
    imageView.setImageBitmap(bitmap);
}

public void rotateImage1() {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    matrix.mapRect(rectF);
    Bitmap targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), bitmap.getConfig());
    Canvas canvas = new Canvas(targetBitmap);
    canvas.drawBitmap(bitmap, matrix, new Paint());
    imageView.setImageBitmap(bitmap);
}

public void rotateImage2() {
    Matrix mtx = new Matrix();
    mtx.reset();
    mtx.preTranslate(20, 30);
    mtx.setRotate((float) 90, 25, 35);
    mtx.postTranslate(30, 40);
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, 100, 100, mtx, true);
    this.bitmap = rotatedBMP;
    imageView.setImageBitmap(bitmap);
}

public void rotateImage3() {
    Bitmap bmpOriginal = bitmap;
    Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()), (bmpOriginal.getHeight()), Bitmap.Config.ARGB_8888);
    Paint p = new Paint();
    p.setAntiAlias(true);

    Matrix matrix = new Matrix();
    matrix.setRotate((float) 90, (float) (bmpOriginal.getWidth() / 2), (float) (bmpOriginal.getHeight() / 2));

    RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
    matrix.mapRect(rectF);

    targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), Bitmap.Config.ALPHA_8);

    Canvas tempCanvas = new Canvas(targetBitmap);
    tempCanvas.drawBitmap(bmpOriginal, matrix, p);
    imageView.setImageBitmap(bmpOriginal);
}

public Bitmap getBitmapFromUri() {
    getContentResolver().notifyChange(imageUri, null);
    ContentResolver cr = getContentResolver();
    Bitmap bitmap;
    try {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
}

我尝试过不同的代码rotateImage、1、2、3等。但没有代码工作。 rotateImage2() 正在工作,但它严重影响质量,所以不能使用它。

我从这里得到这些代码Android:如何以中心点旋转位图 https://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a-center-point那里的用户说它有效,但对我来说不行。 那么我做错了什么。


坚持旋转图像2()并使用较少的转换。这段代码对我有用:

private Bitmap rotateImage(String pathToImage) {

    // 1. figure out the amount of degrees
    int rotation = getImageRotation();

    // 2. rotate matrix by postconcatination
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation);

    // 3. create Bitmap from rotated matrix
    Bitmap sourceBitmap = BitmapFactory.decodeFile(pathToImage);
    return Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);        
}

获取图像旋转查看你的其他帖子 https://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android。有关 Matrix 操作及其差异的更多信息,例如 setRotate 和 postRotate看看这个帖子 https://stackoverflow.com/questions/3855578/android-matrix-what-is-the-different-between-preconcat-and-postconcat.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用于旋转相机意图捕获的图像的代码在 Android 中不起作用 的相关文章

  • “此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 7.0 不兼容。” -反应-原生

    当我尝试运行反应本机应用程序时 我遇到此错误react native run android 我无法安装该应用程序 我正在尝试构建一个相机应用程序 我当前的react native版本 0 62 0 React cli版本 2 0 1 De
  • 在Android中将半径边框绘制到imageview或textview的一个角落

    我需要在我的应用程序中为图像视图或文本视图绘制边框 但我只需要在一个角落绘制它 就像图像一样 我做了一个形状 但我在所有 4 个边上都有边框
  • 删除视图并重新创建它

    有没有办法删除设置的视图 setContentView R layout set map center mapView MapView findViewById R id mapview 如果我再次调用此视图 则会收到一条错误消息 java
  • 在Android内存中存储gif图像

    我对安卓还很陌生 我想将图像保存到内存中 然后从内存中检索图像并将其加载到图像视图中 我已使用以下代码成功将图像存储在内存中 void saveImage String fileName img cnt jpg File file new
  • NumberPicker 的格式化值在单击时消失

    我的 NumberPicker 在setDescendantFocusability FOCUS BLOCK DESCENDANTS 模式和setWrapSelectorWheel false 已关闭 我用一个简单的格式化程序格式化了我的
  • Android浏览器上的Web应用程序宽度问题

    所以到目前为止我只在 Android 浏览器上遇到过这个问题 基本上我的网站几乎一直运行良好 而且我还没有在 Dolphin Opera 或 Skyfire 上看到这个问题 但偶尔当我从手机主屏幕之一上的书签重新打开 Android 浏览器
  • Android/Java 创建辅助类来创建图表

    Goal 创建用于图形生成的辅助类 背景 我有 3 个片段 每个片段收集一些传感器数据 加速度计 陀螺仪 旋转 并使用 GraphView 绘制图表 以下是其中一个片段的代码 该代码当前工作正常 public class Gyroscope
  • Android Accessibility 执行触摸操作

    我想知道是否可以使用 Android 辅助功能服务在屏幕上的位置执行触摸操作 例如 Bundle arguments new Bundle arguments putInt coord X X value arguments putInt
  • Android:拍照后调用裁剪活动

    我在解析拍摄照片的 uri 来裁剪活动时遇到问题 在我的应用程序中 用户可以拍摄一张照片或从图库中选择一张照片 然后裁剪并上传 一切听起来都很简单 从图库中选择时 图库应用程序会返回所选照片的 uri 如下所示 content media
  • 错误 libGL.so:无法使用 Android 模拟器打开共享对象文件

    我试图在 Ubuntu 12 04 64 位 中运行 android 模拟器 但是 我收到以下错误 Starting emulator for AVD emulatr Failed to load libGL so error libGL
  • 错误:任务“:app:mergeDebugResources”执行失败。 > java.lang.ArrayIndexOutOfBoundsException(无错误消息)

    你们有人知道 Gradle 构建中的这个异常吗 Error Execution failed for task app mergeDebugResources gt java lang ArrayIndexOutOfBoundsExcept
  • 当编辑文本获得焦点时更改边框颜色

    我想知道当编辑文本聚焦时如何更改它的边框颜色 目前它看起来像这样 我尝试过在SDK中检查源图片 但我无法理解它 我也尝试过使用xml 但无法仅更改边框颜色 如果我找到源图片 我可以在 Photoshop 中编辑以更改颜色 有什么关于如何执行
  • cordova插件条码扫描仪打不开扫描

    我的条形码扫描仪插件有问题 我不是天才 我不太了解如何编写网络应用程序 我使用phonegap和cordova 并且尝试制作一个网络应用程序 在单击链接后扫描条形码 我之前已经使用此命令行安装了该插件 cordova plugin add
  • 以编程方式应用样式资源

    我没有找到一种以编程方式做到这一点的方法 所以我在这里发布这个问题 我也没有找到与此相关的任何问题 我有一个资源样式 在 res values styles xml 中定义 我想做的是使用 java 将这种样式应用到我正在操作的 View
  • 如何从webkit浏览器中检测Android版本和品牌?

    如何通过webkit浏览器检测Android版本和品牌 可靠吗 我相信你可以检查用户代理 但是 我认为它不安全 因为有很多方法可以用来欺骗用户代理 在谷歌上搜索这个问题给了我们很多答案 它甚至可以在默认浏览器上运行 您只需输入 about
  • 活动组代码示例

    有人可以给我一些使用活动组的示例代码吗 我的应用程序中有一些按钮 我想将活动应用于这些按钮 目前我正在使用 setVisibility 但我被告知活动组将是更好的选择 这是另一个ActivityGroup 示例项目 http richipa
  • 使用 ImageMagick/convert 创建半透明 PNG

    我有 PNG 文件 我想将整个图像转换为半透明 该图像将在 KML 文件中引用为 Google 地球 地图中使用的图标叠加层 使用 ImageMagick 向我建议了以下示例convert命令 但似乎都不起作用 第一个示例会导致错误 usr
  • 确定视图是否在屏幕上 - Android

    我对这个有点困惑 首先也是最重要的是 以下链接很有用 但是我提出了一些可见性问题 链接 检查视图可见性 https stackoverflow com questions 4628800 android how to check if a
  • 在 Android ADT Eclipse 插件中滚动布局编辑器

    有谁知道当布局编辑器的内容溢出一个 屏幕 时如何滚动这些内容 我说的是在设计时使用 ADT 布局编辑器 而不是在物理设备上运行时滚动 效果很好 关闭 Android 布局编辑器中的剪辑 切换剪辑 按钮位于 Android 布局编辑器的右上角
  • 在DialogFragment中,onCreate应该做什么?

    我目前正在摆弄 DialogFragment 以学习使用它 我假设相比onCreateView onCreate 可以这样做 public void onCreate Bundle savedInstanceState super onCr

随机推荐