如何知道一个图像文件的类型 [英] How to know the type of a image file

查看:227
本文介绍了如何知道一个图像文件的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于文件路径(不带扩展名)。我想知道,如果文件中的图像是JPEG或PNG。

Given a file path (without extension) I'd like to know if the image inside the file is a JPEG or a PNG.

我该怎么办呢?

推荐答案

您可以找到它是这样的:

You can find it like this:

第1步:你刚刚得到的图像边界只有....

step 1: You just get the image bounds only....

第2步:您可以使用下面的方法,你的相应尺寸改变你的形象

step 2: You can change your image with your corresponding size using the below method

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    in.mark(in.available());
    BitmapFactory.decodeStream(in, null, options);
    (or)
    BitmapFactory.decodeFile(pathName);
    (or)
    BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.ic_launcher);
    String name = options.outMimeType;

这下面的方法来获得较大的位图装载带有效 你需要的高度和宽度

this below method is used to get the large bitmaps loading with efficiently re-sizing the image with your required height and width

public static Bitmap decodeSampledBitmapFromResource(InputStream in,
            int reqWidth, int reqHeight) throws IOException {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        in.mark(in.available());
        BitmapFactory.decodeStream(in, null, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        in.reset();
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(in, null, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

这篇关于如何知道一个图像文件的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆