下面列出了android.media.ThumbnailUtils#extractThumbnail ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 获取图片缩略图
*/
public static Bitmap getPictureImage(String urlPath) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(urlPath, options);
options.inJustDecodeBounds = false;
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / 100;
int beHeight = h / 80;
int be = 1;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
bitmap = BitmapFactory.decodeFile(urlPath, options);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 80,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
private float[] getPixels(Bitmap bitmap) {
int[] intValues = new int[IMAGE_SIZE * IMAGE_SIZE];
float[] floatValues = new float[IMAGE_SIZE * IMAGE_SIZE * 3];
if (bitmap.getWidth() != IMAGE_SIZE || bitmap.getHeight() != IMAGE_SIZE) {
// rescale the bitmap if needed
bitmap = ThumbnailUtils.extractThumbnail(bitmap, IMAGE_SIZE, IMAGE_SIZE);
}
bitmap.getPixels(intValues,0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
floatValues[i * 3] = Color.red(val) / 255.0f;
floatValues[i * 3 + 1] = Color.green(val) / 255.0f;
floatValues[i * 3 + 2] = Color.blue(val) / 255.0f;
}
return floatValues;
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
if (bm == null) {
mPaint.reset();
mPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
invalidate();
return;
} else {
mPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.white));
}
Bitmap centerCroppedBitmap = ThumbnailUtils.extractThumbnail(bm, mSide, mSide);
BitmapShader shader = new BitmapShader(centerCroppedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
}
public static Bitmap createVideoThumbnail(String filePath, int width, int height, int offsetMillSecond) {
Bitmap bitmap = createVideoThumbnail(filePath, offsetMillSecond);
if (bitmap != null) {
return ThumbnailUtils.extractThumbnail(bitmap, width, height);
}
return bitmap;
}
public static Bitmap createVideoThumbnail(String filePath, int width, int height) {
Bitmap bp = createVideoThumbnail(filePath);
if (bp != null) {
return ThumbnailUtils.extractThumbnail(bp, width, height);
}
return bp;
}
public static Bitmap createVideoThumbnail(String filePath, int width, int height) {
Bitmap bp = createVideoThumbnail(filePath);
if (bp != null) {
return ThumbnailUtils.extractThumbnail(bp, width, height);
}
return bp;
}
/**
* 保存图片
* @param
* @return 解析流生成的缩略图
*/
public Bitmap save(byte[] data){
if(data!=null){
//解析生成相机返回的图片
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
//获取加水印的图片
bm=getBitmapWithWaterMark(bm);
//生成缩略图
Bitmap thumbnail=ThumbnailUtils.extractThumbnail(bm, 213, 213);
//产生新的文件名
String imgName=FileOperateUtil.createFileNmae(".jpg");
String imagePath=mImageFolder+File.separator+imgName;
String thumbPath=mThumbnailFolder+File.separator+imgName;
File file=new File(imagePath);
File thumFile=new File(thumbPath);
try{
//存图片大图
FileOutputStream fos=new FileOutputStream(file);
ByteArrayOutputStream bos=compress(bm);
fos.write(bos.toByteArray());
fos.flush();
fos.close();
//存图片小图
BufferedOutputStream bufferos=new BufferedOutputStream(new FileOutputStream(thumFile));
thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, bufferos);
bufferos.flush();
bufferos.close();
return bm;
}catch(Exception e){
Log.e(TAG, e.toString());
Toast.makeText(getContext(), "解析相机返回流失败", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getContext(), "拍照失败,请重试", Toast.LENGTH_SHORT).show();
}
return null;
}
public static Bitmap getVideoThumbnail(String var0, int var1, int var2, int var3) {
Bitmap var4 = null;
var4 = ThumbnailUtils.createVideoThumbnail(var0, var3);
// EMLog.d("getVideoThumbnail", "video thumb width:" + var4.getWidth());
// EMLog.d("getVideoThumbnail", "video thumb height:" + var4.getHeight());
var4 = ThumbnailUtils.extractThumbnail(var4, var1, var2, 2);
return var4;
}
public static String convertToBase64(Bitmap bitmap, int maxWidth, int maxHeight) {
if (bitmap == null) {
return null;
}
long startTime = System.currentTimeMillis();
int[] targetSize = computeTargetSize(bitmap, maxWidth, maxHeight);
// 10-100ms量级
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, targetSize[0], targetSize[1]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, baos);
String result = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
// L.d("ImageUtil.convertToBase64 cost %s ms", (System.currentTimeMillis() - startTime));
return result;
}
public static Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;
BitmapFactory.decodeFile(pathName, opts);
// 得到图片的宽度、高度;
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
int ratio = 1;
if (widthRatio < heightRatio) {
ratio = widthRatio;
} else {
ratio = heightRatio;
}
if (ratio <= 0) {
ratio = 1;
}
opts.inSampleSize = ratio;
// 设置好缩放比例后,加载图片进内容;
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, targetWidth, targetHeight,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
private Bitmap getRoundBitmap(@DrawableRes int drawable, int size) {
Bitmap bitmap = ImageUtils.drawableToBitmap(ContextCompat.getDrawable(getContext(), drawable));
bitmap = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 6, bitmap.getHeight() / 6, (int) (0.666 * bitmap.getWidth()), (int) (0.666 * bitmap.getHeight()));
bitmap = ThumbnailUtils.extractThumbnail(bitmap, size, size);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(size / 2);
roundedBitmapDrawable.setAntiAlias(true);
return ImageUtils.drawableToBitmap(roundedBitmapDrawable);
}
@Override
public void onAnimtionEnd(Bitmap bm,boolean isVideo) {
if(bm!=null){
//生成缩略图
Bitmap thumbnail=ThumbnailUtils.extractThumbnail(bm, 213, 213);
mThumbView.setImageBitmap(thumbnail);
if(isVideo)
mVideoIconView.setVisibility(View.VISIBLE);
else {
mVideoIconView.setVisibility(View.GONE);
}
}
}
@Override
public void onDraw(Canvas canvas) {
Bitmap image = ImageUtils.drawableToBitmap(getDrawable());
if (image != null) {
int size = Math.min(getWidth(), getHeight());
image = ThumbnailUtils.extractThumbnail(image, size, size);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), image);
roundedBitmapDrawable.setCornerRadius(size / 2);
roundedBitmapDrawable.setAntiAlias(true);
canvas.drawBitmap(ImageUtils.drawableToBitmap(roundedBitmapDrawable), 0, 0, paint);
}
}
private Bitmap getBitmap(int size, @DrawableRes int resource) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
return ThumbnailUtils.extractThumbnail(BitmapFactory.decodeResource(getResources(), resource, options), size, size);
}
@SuppressLint("ServiceCast")
public void run() {
while (flag) {
try {
// Thread.sleep(30000);
Random random = new Random();
int number = random.nextInt(urlStrings.length);
byte[] data = readInputStream(new URL(urlStrings[number]).openStream());
if (data.length>0 && data.length<50) {
data = readInputStream(new URL(urlStrings[number].replace("720x1280", "640x960")).openStream());
}else if(data==null || data.length==0){
number = random.nextInt(urlStrings.length);
data = readInputStream(new URL(urlStrings[number]).openStream());
}
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Log.i("kyson", bitmap.getWidth() + "========");
WallpaperManager wpm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
WindowManager wm = (WindowManager) WallWrapperApplication.getContext().getSystemService(Context.WINDOW_SERVICE);
@SuppressWarnings("deprecation")
int width = wm.getDefaultDisplay().getWidth();
@SuppressWarnings("deprecation")
int height = wm.getDefaultDisplay().getHeight();
wpm.suggestDesiredDimensions(width, height);
Bitmap bitmap2 = ThumbnailUtils.extractThumbnail(bitmap, width, height);
WallpaperManager.getInstance(WallWrapperApplication.getContext()).setBitmap(bitmap2);
Thread.sleep(60000);
} catch (Exception ex) {
ex.printStackTrace();
}
/*
* try { Thread.sleep(3000);
*
* String fileString =
* WallWrapperApplication.getContext().getExternalFilesDir
* (Environment.DIRECTORY_DCIM).getPath(); File files = new
* File(fileString); Random r = new Random(); if
* (files.isDirectory()) { int nums = files.listFiles().length;
* if (nums > 0) { int i = r.nextInt(nums); try {
* FileInputStream fis = new
* FileInputStream(files.listFiles()[i]); WallpaperManager wpm =
* (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
* WindowManager wm = (WindowManager)
* WallWrapperApplication.getContext
* ().getSystemService(Context.WINDOW_SERVICE);
*
* @SuppressWarnings("deprecation") int width =
* wm.getDefaultDisplay().getWidth();
*
* @SuppressWarnings("deprecation") int height =
* wm.getDefaultDisplay().getHeight();
* wpm.suggestDesiredDimensions(width, height);
*
* @SuppressWarnings("deprecation") BitmapDrawable pic = new
* BitmapDrawable(fis); Bitmap bitmap = pic.getBitmap(); bitmap
* = ThumbnailUtils.extractThumbnail(bitmap, width, height);
* WallpaperManager
* .getInstance(WallWrapperApplication.getContext
* ()).setBitmap(bitmap); } catch (FileNotFoundException e) {
* e.printStackTrace(); } catch (IOException e) {
* e.printStackTrace(); } } }
*
* } catch (Exception ex) { ex.printStackTrace(); }
*/
}
}
private Bitmap getImageThumbnail(Bitmap bitmap) {
return ThumbnailUtils.extractThumbnail(bitmap, 320, 320, OPTIONS_RECYCLE_INPUT);
}
public static Bitmap getThumb(Bitmap source, int width, int height) {
return ThumbnailUtils.extractThumbnail(source, width, height);
}
/**
* 获取视频的缩略图
* 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
* 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
*
* @param videoPath 视频的路径
* @param width 指定输出视频缩略图的宽度
* @param height 指定输出视频缩略图的高度度
* @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
* @return 指定大小的视频缩略图
*/
private Bitmap createVideoThumbBitmap(String videoPath, int width, int height, int kind)
{
Bitmap bitmap = null;
// 获取视频的缩略图
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
/**
* 对图片进行 毛玻璃化,虚化
* @param originBitmap 位图
* @param width 缩放后的期望宽度
* @param height 缩放后的期望高度
* @param blurRadius 虚化程度
* @return 位图
*/
public static Bitmap doBlur(Bitmap originBitmap,int width,int height,int blurRadius){
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(originBitmap, width, height);
Bitmap blurBitmap = doBlur(thumbnail, blurRadius, true);
thumbnail.recycle();
return blurBitmap;
}
/**
* 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
* 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
*
* @param videoPath
* 视频的路径
* @param width
* 指定输出视频缩略图的宽度
* @param height
* 指定输出视频缩略图的高度度
* @param kind
* 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
* 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
* @return 指定大小的视频缩略图
*/
public static Bitmap getVideoThumbnail(String videoPath, int width,
int height, int kind) {
Bitmap bitmap = null;
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
if (width > 0 && height > 0) {
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}