android获取在线视频略缩图

文章参考:

http://blog.csdn.net/u011198764/article/details/40540607

提取了所需文件和代码,留作备忘。

/**
	 * @param url
	 *            要加载资源的地址
	 * @param imageView
	 *            显示略缩图的ImageView控件
	 */
	public void loadVideoBitmap(String url, ImageView imageView) {
		// 缓存中的Bitmap
		Bitmap bitmap = ImageDownloader.getBitmapFromCache(url);
		// 缓存中不存在就从网络上获取资源
		if (bitmap == null) {
			forceDownload(url, imageView);
		} else {
			imageView.setVisibility(View.VISIBLE);
			ImageDownloader.cancelPotentialDownload(url, imageView);
			imageView.setImageBitmap(adjustPhotoRotation(ThumbnailUtils.extractThumbnail(bitmap,
					100, 100),90));
		}
	}

	/**
	 * @param url
	 *            资源地址
	 * @param imageView
	 *            加载略缩图的ImageView控件
	 */
	private void forceDownload(String url, ImageView imageView) {
		// State sanity: url is guaranteed to never be null in
		// DownloadedDrawable and cache keys.
		if (url == null) {
			imageView.setImageDrawable(null);
			return;
		}
		// 判断是否需要下载
		if (ImageDownloader.cancelPotentialDownload(url, imageView)) {
			imageView.setVisibility(View.VISIBLE);
			BitmapWorkerTask task = new BitmapWorkerTask(imageView);
			Bitmap defaultBimap = BitmapFactory.decodeStream(mContext
					.getResources().openRawResource(R.drawable.image_failed1));
			AsyncDrawable downloadedDrawable = new AsyncDrawable(
					mContext.getResources(), defaultBimap, task);
			imageView.setImageDrawable(downloadedDrawable);
			imageView.setMinimumHeight(100);
			task.execute(url);
		}
	}

	class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
		private final WeakReference<ImageView> imageViewReference;
		private String url;

		public BitmapWorkerTask(ImageView imageView) {
			// Use a WeakReference to ensure the ImageView can be garbage
			// collected
			imageViewReference = new WeakReference<ImageView>(imageView);
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			url = params[0];
			// 获取在线视频的帧的图像,返回Bitmap
			Bitmap tempBitmap = getVideoThumbnail(url);
			return tempBitmap;
		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			if (isCancelled()) {
				bitmap = null;
			}

			ImageDownloader.addBitmapToCache(url, bitmap);
			if (imageViewReference != null && bitmap != null) {
				final ImageView imageView = imageViewReference.get();
				final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
				// 判断当前的异步对象是否与ImageView所在的异步对象是否相等,以此来防止加载错乱
				if (this == bitmapWorkerTask && imageView != null) {

					//
					imageView.setTag(bitmap);
					imageView.setImageBitmap(adjustPhotoRotation(
							ThumbnailUtils.extractThumbnail(bitmap, 100, 100),
							90));
				}
			}
		}
	}

	public Bitmap getVideoThumbnail(String videoUrl) {
		Bitmap generateBitmap = null;
		FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever();
		try {
			fmmr.setDataSource(videoUrl);
			generateBitmap = fmmr.getFrameAtTime();

			if (generateBitmap != null) {
				Bitmap b2 = fmmr.getFrameAtTime(4000000,
						FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC);
				if (b2 != null) {
					// 获取到了Bitmap之后用android自带的ThumbnailUtils获取自定义大小的略缩图
					generateBitmap = ThumbnailUtils.extractThumbnail(b2, 640,
							640, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
				}
			}
		} catch (IllegalArgumentException ex) {
			ex.printStackTrace();
		} finally {
			fmmr.release();
		}
		if (generateBitmap != null) {
			return generateBitmap;
		}
		return null;
	}

	static class AsyncDrawable extends BitmapDrawable {

		private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;

		public AsyncDrawable(Resources res, Bitmap bitmap,

		BitmapWorkerTask bitmapWorkerTask) {

			super(res, bitmap);

			bitmapWorkerTaskReference =

			new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);

		}

		public BitmapWorkerTask getBitmapWorkerTask() {

			return bitmapWorkerTaskReference.get();

		}

	}

	private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {

		if (imageView != null) {

			final Drawable drawable = imageView.getDrawable();

			if (drawable instanceof AsyncDrawable) {

				final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;

				return asyncDrawable.getBitmapWorkerTask();

			}

		}

		return null;

	}

	/**
	 * 旋转图片
	 * 
	 * @param bm
	 * @param orientationDegree
	 * @return
	 */
	private Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

		Matrix m = new Matrix();
		m.setRotate(orientationDegree, (float) bm.getWidth() / 2,
				(float) bm.getHeight() / 2);
		float targetX, targetY;
		if (orientationDegree == 90) {
			targetX = bm.getHeight();
			targetY = 0;
		} else {
			targetX = bm.getHeight();
			targetY = bm.getWidth();
		}

		final float[] values = new float[9];
		m.getValues(values);

		float x1 = values[Matrix.MTRANS_X];
		float y1 = values[Matrix.MTRANS_Y];

		m.postTranslate(targetX - x1, targetY - y1);

		Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(),
				Bitmap.Config.ARGB_8888);
		Paint paint = new Paint();
		Canvas canvas = new Canvas(bm1);
		canvas.drawBitmap(bm, m, paint);

		return bm1;
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值