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;
}
}
缩略图采集工具类
最新推荐文章于 2017-08-22 10:59:46 发布
本文介绍了一种用于创建图片和视频缩略图的方法,包括对图片进行二次采样以防止内存溢出,从视频文件中提取典型帧作为封面,以及从音乐文件中读取内置的专辑图片。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Anything-LLM
AI应用
AnythingLLM是一个全栈应用程序,可以使用商用或开源的LLM/嵌入器/语义向量数据库模型,帮助用户在本地或云端搭建个性化的聊天机器人系统,且无需复杂设置
2445

被折叠的 条评论
为什么被折叠?



