Android:检查位图是否具有4:3的宽高比

时间:2016-07-28 08:48:30

标签: android bitmap

我正在使用内容解析器

从图库中检索图像
  final String[] columns = {MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media._ID};
            final String orderBy = MediaStore.Images.Media._ID;
            Cursor imagecursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy);
            int image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);


    count = imagecursor.getCount();
            imageList = new ArrayList<Bitmap>();


    for (int i = 0; i < count; i++) {

  imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
        Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(
                context.getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);
    imageList.add(b);

    }

如何检查位图宽高比是否为4:3,然后添加到arraylist?

2 个答案:

答案 0 :(得分:6)

您只需要检查位图的宽度和高度。

float RATIO = 4 / 3;
float EPSILON = 0.00001f;
float ratio = (float)bitmap.getWidth() / bitmap.getHeight();
if (Math.abs(ratio - RATIO) < EPSILON) {
    list.add(bitmap);
}

答案 1 :(得分:0)

试试这个:

if ((double) b.getHeight() / b.getWidth() == (double) 4 / 3) {
                    // ratio is 4:3, now you can add to ArrayList
                } else {
                    // ratio is not 4:3
                }