Android - 如何压缩或缩小图像?

发布于 2024-10-17 03:06:18 字数 1331 浏览 1 评论 0原文

ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageResource(R.drawable.avatar);
strAvatarFilename =  "Image.jpg"; 
final Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/Folder/"+strAvatarFilename));
avatarButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        pictureIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUriToSaveCameraImageTo );
        startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
        Editor editor = Preferences.edit();
        editor.putString(PREFERENCES_AVATAR, imageUriToSaveCameraImageTo.getPath());
        editor.commit();
    }
});

我有这个代码。它拍摄一张照片,然后将其存储在两个位置,SD 卡上的默认位置和我希望将照片存储在的 /folder/ 文件。拍摄后,我使用此刷新屏幕...

ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
String strAvatarUri = Preferences.getString(PREFERENCES_AVATAR, "");
Uri imageUri = Uri.parse(strAvatarUri);
avatarButton.setImageURI(null);
avatarButton.setImageURI(imageUri);

这会刷新图像按钮,以便用户可以看到他们拍摄的图像。然而,它看起来很大并且填满了屏幕。有什么方法可以压缩或缩小图像,以便用户可以以合理的尺寸查看它?谢谢

ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageResource(R.drawable.avatar);
strAvatarFilename =  "Image.jpg"; 
final Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/Folder/"+strAvatarFilename));
avatarButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        pictureIntent.putExtra( MediaStore.EXTRA_OUTPUT, imageUriToSaveCameraImageTo );
        startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
        Editor editor = Preferences.edit();
        editor.putString(PREFERENCES_AVATAR, imageUriToSaveCameraImageTo.getPath());
        editor.commit();
    }
});

I have this code. It takes a photo and then stores it in two locations, the default location on the SDcard and the /folder/ file i want the photo to be stored in also. Once taken i refresh the screen using this...

ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
String strAvatarUri = Preferences.getString(PREFERENCES_AVATAR, "");
Uri imageUri = Uri.parse(strAvatarUri);
avatarButton.setImageURI(null);
avatarButton.setImageURI(imageUri);

This refreshes the image button so the user can see the image they have taken. However, this is appears very big and fills the screen. Is there any way i can compress or downsize the image so the user can see it at a reasonable size? thanks

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

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

发布评论

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

评论(3

涫野音 2024-10-24 03:06:18
Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath), 96, 96, false);

其中 createScaledBitmap 的第二个和第三个参数分别是宽度和高度。

http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29

编辑:我现在想到了您可以使用 BitmapFactory.OptionsinSampleSize 选项以更快、更有效的方式完成此操作:

BitmapFactory.Options opts = new BitmapFactory.Options ();
opts.inSampleSize = 2;   // for 1/2 the image to be loaded
Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath, opts), 96, 96, false);

这样您将节省更多内存,并且缩小规模应该更少的时间。

Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath), 96, 96, false);

Where the second and third parameters to createScaledBitmap are the width and height respectively.

http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29

EDIT: It now occurred to me that you can do this in a faster, more efficient way, using BitmapFactory.Options and the inSampleSize option:

BitmapFactory.Options opts = new BitmapFactory.Options ();
opts.inSampleSize = 2;   // for 1/2 the image to be loaded
Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath, opts), 96, 96, false);

This way you will save even more memory and the downsizing should take less time.

感性 2024-10-24 03:06:18

您可以修改 ImageButton 的scaleType 属性(例如fitXY 或cropCenter),并且通过将固定大小设置为LayoutPreferences(高度和宽度),您就可以完成所有设置。

You can mess with the ImageButton's scaleType properties (to fitXY for example, or cropCenter), and by setting a fixed size to LayoutPreferences (height and width) you'll be all set.

内心激荡 2024-10-24 03:06:18

Android 库 SiliCompressor 提供此功能,让您在保持图像质量的同时压缩图像。

The Android Library SiliCompressor provide this functionality for you to compress your images while keeping the image quality.

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