返回

简析如何轻松准确获取手机内图片分辨率宽高

Android

获取手机内图片分辨率宽高的方法

获取手机内图片的分辨率(宽度和高度)有多种方法。在本文中,我们将介绍两种最常见的方法:

  1. 使用BitmapFactory.decodeFile()方法

BitmapFactory.decodeFile()方法是Android中获取图片分辨率最常用的方法。该方法接受一个文件路径作为参数,并返回一个Bitmap对象。Bitmap对象包含有关图片的信息,包括其宽度和高度。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
int width = options.outWidth;
int height = options.outHeight;
  1. 使用ExifInterface类

ExifInterface类是Android中获取图片元数据(包括分辨率)的类。要使用ExifInterface类,您需要创建一个ExifInterface对象,并使用该对象获取图片的分辨率。

ExifInterface exif = new ExifInterface(filePath);
int width = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
int height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);

在iOS中获取手机内图片分辨率宽高

在iOS中,您可以使用UIImagePickerController类来获取图片的分辨率。UIImagePickerController类是一个允许您从相册或相机中选择图片的类。要使用UIImagePickerController类,您需要创建一个UIImagePickerController对象,并使用该对象选择图片。

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[picker presentModalViewController:self animated:YES];

当用户选择一张图片后,您可以使用UIImagePickerControllerDelegate协议来获取图片的分辨率。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    int width = image.size.width;
    int height = image.size.height;
}

总结

在本文中,我们介绍了如何获取手机内图片的分辨率,包括宽度和高度。我们讨论了在Android和iOS平台上实现此操作的不同方法。我们还提供了一些代码示例,以帮助您轻松实现此功能。