缩略图采集工具类

package com.lt.an20_utils;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
/**
 * Created by 风情万种冷哥哥 on 2016.
 *
 */
public class BitmapThumbnailHelper {

    /**
     * 对图片进行二次采样,生成缩略图。放置加载过大图片出现内存溢出
     */
    public static Bitmap createThumbnail(byte[] data, int newWidth,
            int newHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, options);
        int oldWidth = options.outWidth;
        int oldHeight = options.outHeight;

        // Log.i("Helper", "--->oldWidth:" + oldWidth);
        // Log.i("Helper", "--->oldHeight:" + oldHeight);

        int ratioWidth = 0;
        int ratioHeight = 0;

        if (newWidth != 0 && newHeight == 0) {
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioWidth;
            // Log.i("Helper", "--->ratioWidth:" + ratioWidth);

        } else if (newWidth == 0 && newHeight != 0) {
            ratioHeight = oldHeight / newHeight;
            options.inSampleSize = ratioHeight;
        } else {
            ratioHeight = oldHeight / newHeight;
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
                    : ratioWidth;
        }
        options.inPreferredConfig = Config.ALPHA_8;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory
                .decodeByteArray(data, 0, data.length, options);
        return bm;
    }

    public static Bitmap createThumbnail(String pathName, int newWidth,
            int newHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);
        int oldWidth = options.outWidth;
        int oldHeight = options.outHeight;

        int ratioWidth = 0;
        int ratioHeight = 0;

        if (newWidth != 0 && newHeight == 0) {
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioWidth;
        } else if (newWidth == 0 && newHeight != 0) {
            ratioHeight = oldHeight / newHeight;
            options.inSampleSize = ratioHeight;
        } else {
            ratioHeight = oldHeight / newHeight;
            ratioWidth = oldWidth / newWidth;
            options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
                    : ratioWidth;
        }
        options.inPreferredConfig = Config.ALPHA_8;
        options.inJustDecodeBounds = false;
        Bitmap bm = BitmapFactory.decodeFile(pathName, options);
        return bm;
    }

    // 获取视频文件的典型帧作为封面
    public static Bitmap createVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime();
        } catch (Exception ex) {
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
            }
        }
        return bitmap;
    }

    // 获取音乐文件中内置的专辑图片
    public static Bitmap createAlbumThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            byte[] art = retriever.getEmbeddedPicture();
            bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
        } catch (Exception ex) {
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
            }
        }
        return bitmap;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Delphi 实现批量缩略图生成工具的示例代码: ``` uses FMX.Graphics, System.IOUtils; procedure GenerateThumbnail(const ASourceFile, ATargetFile: string; const AWidth, AHeight: Integer); var Bitmap: TBitmap; ThumbBitmap: TBitmap; Scale: Single; begin Bitmap := TBitmap.Create; try Bitmap.LoadFromFile(ASourceFile); if (Bitmap.Width > AWidth) or (Bitmap.Height > AHeight) then begin Scale := Min(AWidth / Bitmap.Width, AHeight / Bitmap.Height); ThumbBitmap := Bitmap.CreateThumbnail(Round(Bitmap.Width * Scale), Round(Bitmap.Height * Scale)); end else ThumbBitmap := Bitmap; ThumbBitmap.SaveToFile(ATargetFile); finally Bitmap.Free; ThumbBitmap.Free; end; end; procedure GenerateThumbnails(const ASourceFolder, ATargetFolder: string; const AWidth, AHeight: Integer); var Files: TStringDynArray; I: Integer; SourceFile, TargetFile: string; begin Files := TDirectory.GetFiles(ASourceFolder, '*.*', TSearchOption.soAllDirectories); for I := 0 to High(Files) do begin SourceFile := Files[I]; TargetFile := TPath.Combine(ATargetFolder, TPath.ChangeExtension(TPath.GetRelativePath(ASourceFolder, SourceFile), '.jpg')); GenerateThumbnail(SourceFile, TargetFile, AWidth, AHeight); end; end; ``` 该示例代码使用了 FMX.Graphics 单元中的 TBitmap 类来加载、缩放和保存图像。GenerateThumbnail 过程接受源文件路径和目标文件路径,以及所需的缩略图宽度和高度。如果源图像的宽度或高度大于所需的尺寸,则会创建一个缩略图,否则直接使用原始图像。GenerateThumbnails 过程接受源文件夹路径和目标文件夹路径,以及所需的缩略图宽度和高度。它会递归地查找源文件夹中的所有文件,并为每个文件生成一个缩略图,将其保存到目标文件夹中,使用与源文件相同的相对路径和 .jpg 扩展名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值