如何知道一个图像文件的类型图像文件、类型

2023-09-06 22:54:12 作者:喵了个汪

由于文件路径(不带扩展名)。我想知道,如果文件中的图像是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;
    }