保存旋转位图 android

发布于 2024-10-19 22:20:46 字数 837 浏览 2 评论 0原文

(如果我完全错了,请原谅我,我是新手) 我正在显示一些使用 MediaStore.ACTION_IMAGE_CAPTURE 拍摄的照片。我尝试在拍照时禁用自动方向,但它似乎不起作用,我使用

putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)

这迫使我旋转一些拍摄的照片。然后我将这些照片保存到 SDCARD。我的问题是我不想在每次用户加载照片时旋转它们。我尝试使用此代码创建一个新的位图,该位图将以“旋转”状态保存。它可以在模拟器上运行,但在我的 HTC 上崩溃。我认为这是一个内存问题。有什么办法可以有效地做到这一点吗?更好的是,有什么方法可以在使用相机意图拍照时真正禁用自动方向吗?

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg");
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
     Matrix mtx = new Matrix();
     mtx.postRotate(90);
     Bitmap rotatedBitmap = Bitmap.createBitmap(Bitmap.createBitmap(tempBitmap, 0, 0, 
                                                         tempW, tempH, mtx, true));

} else{
  //...
}

(Forgive me if Im getting this totally wrong, I am a newbie)
I am displaying some photos taken with MediaStore.ACTION_IMAGE_CAPTURE. I tried to disable autoorientation when picture is being taken but it doesnt seem to work, i used

putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)

This forced me to rotate some of the taken photos. I then save these photos to SDCARD. My problem is that i dont want to rotate them every time user loads a photo. I tried this code to create a new Bitmap that would be saved in 'rotated' state. It worked on emulator but crashes on my HTC. I assume its a memory problem. Is there any way to do this efficiently? Better yet, is there any way to really disable autoorientation while taking photos with Camera Intent?

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg");
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
     Matrix mtx = new Matrix();
     mtx.postRotate(90);
     Bitmap rotatedBitmap = Bitmap.createBitmap(Bitmap.createBitmap(tempBitmap, 0, 0, 
                                                         tempW, tempH, mtx, true));

} else{
  //...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思念绕指尖 2024-10-26 22:20:46

与上面相同,但他忘记了最后一行的一些代码

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; //1/4 of the original image

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg", options);
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
Matrix mtx = new Matrix();
mtx.postRotate(90);
Bitmap rotatedBitmap = **Bitmap.createBitmap**(tempBitmap, 0,0,tempW, tempH, mtx, true);

Same as above but he forgot some code on the last line

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; //1/4 of the original image

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg", options);
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
Matrix mtx = new Matrix();
mtx.postRotate(90);
Bitmap rotatedBitmap = **Bitmap.createBitmap**(tempBitmap, 0,0,tempW, tempH, mtx, true);
玉环 2024-10-26 22:20:46

尝试减少您正在使用的图像,这可能是内存问题。请参阅下文了解可能的解决方案。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; //1/4 of the original image

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg", options);
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    Bitmap rotatedBitmap = (tempBitmap, 0,0,tempW, tempH, mtx, true);

try reducing the image that you are working with it may be a memory issue. See below for possible solution.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; //1/4 of the original image

tempBitmap=BitmapFactory.decodeFile(dirPath+filename+".jpg", options);
int tempW = tempBitmap.getWidth();
int tempH = tempBitmap.getHeight();

if (tempW>tempH) {
    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    Bitmap rotatedBitmap = (tempBitmap, 0,0,tempW, tempH, mtx, true);
倥絔 2024-10-26 22:20:46

通过以下代码,

要停止图像的分级,请使用以下代码 -

private int getImageOrientation() 
{
    final String[] imageColumns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            imageColumns, null, null, imageOrderBy);

    if (cursor.moveToFirst()) {
        int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
        System.out.println("orientation===" + orientation);
        cursor.close();
        return orientation;
    } else {
        return 0;
    }
}

如果您遇到任何问题,请参阅以下链接

http://androidlift.info/2016/01/07/android-camera-image-capturing-and-uploading-to-php-server/

Go through following code,

For Stop ratation of image use following code -

private int getImageOrientation() 
{
    final String[] imageColumns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            imageColumns, null, null, imageOrderBy);

    if (cursor.moveToFirst()) {
        int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
        System.out.println("orientation===" + orientation);
        cursor.close();
        return orientation;
    } else {
        return 0;
    }
}

If you are facing any issue please refer following link

http://androidlift.info/2016/01/07/android-camera-image-capturing-and-uploading-to-php-server/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文