android 截屏




1、网上流传的view.getDrawingCache(); 截取带视频的页是黑框。 那么怎么太能截取上的,我的思路是获取视频的当前一帧,算好坐标,把view.getDrawingCache();获取的图片与当前帧合成一张图。(上图坐标算的不是很准确,但是结果是实现了的)

2、代码如下:

package com.example.screenshot;

import java.io.File;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends ActionBarActivity {

	private VideoView myVideoView;

	MediaMetadataRetriever mediaMetadataRetriever;
	MediaController myMediaController;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mediaMetadataRetriever = new MediaMetadataRetriever();
		mediaMetadataRetriever.setDataSource("/storage/emulated/0/DCIM/Camera/222.mp4");

		myVideoView = (VideoView) findViewById(R.id.videoView1);
		myVideoView.setVideoURI(Uri.parse("/storage/emulated/0/DCIM/Camera/222.mp4"));

		myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
		myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
		myVideoView.setOnErrorListener(myVideoViewErrorListener);

		myVideoView.requestFocus();
		myVideoView.start();

		Log.d("yanglei", Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "DCIM" + File.separator + "111.mp4");

		myVideoView.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		if (id == R.id.action_settings_3) {
			String name = System.currentTimeMillis() + ".png";
			String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + name;
			Toast.makeText(this, path, Toast.LENGTH_LONG).show();
			TextView txt = (TextView) findViewById(R.id.path);
			txt.setText("请去:" + path + "看截图结果");
			Bitmap bg = ScreenShot.takeScreenShot(this);

			int currentPosition = myVideoView.getCurrentPosition(); //in millisecond
			Toast.makeText(MainActivity.this, "Current Position: " + currentPosition + " (ms)", Toast.LENGTH_LONG).show();

			Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(currentPosition * 1000); //unit in microsecond

			if (bmFrame == null) {
				Toast.makeText(MainActivity.this, "bmFrame == null!", Toast.LENGTH_LONG).show();
			} else {
				Matrix matrix = new Matrix();
				float scalex = Float.valueOf(myVideoView.getWidth()) / Float.valueOf(bmFrame.getWidth());
				float scaley = Float.valueOf(myVideoView.getHeight()) / Float.valueOf(bmFrame.getHeight());
				matrix.postScale(scalex, scaley);

				Bitmap foreground = Bitmap.createBitmap(bmFrame, 0, 0, bmFrame.getWidth(), bmFrame.getHeight(), matrix, true);

				bmFrame.recycle();
				Bitmap result = toConformBitmap(bg, foreground, myVideoView);

				AlertDialog.Builder myCaptureDialog = new AlertDialog.Builder(MainActivity.this);
				ImageView capturedImageView = new ImageView(MainActivity.this);
				capturedImageView.setImageBitmap(result);
				LayoutParams capturedImageViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				capturedImageView.setLayoutParams(capturedImageViewLayoutParams);

				myCaptureDialog.setView(capturedImageView);
				myCaptureDialog.show();
			}
			myVideoView.stopPlayback();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	MediaPlayer.OnCompletionListener myVideoViewCompletionListener = new MediaPlayer.OnCompletionListener() {

		@Override
		public void onCompletion(MediaPlayer arg0) {
			Toast.makeText(MainActivity.this, "End of Video", Toast.LENGTH_LONG).show();
		}
	};

	MediaPlayer.OnPreparedListener MyVideoViewPreparedListener = new MediaPlayer.OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer mp) {

			long duration = myVideoView.getDuration(); //in millisecond
			Toast.makeText(MainActivity.this, "Duration: " + duration + " (ms)", Toast.LENGTH_LONG).show();

		}
	};

	MediaPlayer.OnErrorListener myVideoViewErrorListener = new MediaPlayer.OnErrorListener() {

		@Override
		public boolean onError(MediaPlayer mp, int what, int extra) {

			Toast.makeText(MainActivity.this, "Error!!!", Toast.LENGTH_LONG).show();
			return true;
		}
	};

	private Bitmap toConformBitmap(Bitmap background, Bitmap foreground, View videoView) {
		if (background == null) {
			return null;
		}

		int bgWidth = background.getWidth();
		int bgHeight = background.getHeight();

		Bitmap newbmp = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888);
		Canvas cv = new Canvas(newbmp);

		cv.drawBitmap(background, 0, 0, null);//在 0,0坐标开始画入bg   

		int[] location = new int[2];
		videoView.getLocationOnScreen(location);
		int x = location[0];
		int y = location[1];
		cv.drawBitmap(foreground, x, y, null);//在 0,0坐标开始画入fg ,可以从任意位置画入

		cv.save(Canvas.ALL_SAVE_FLAG);//保存   

		cv.restore();//存储   
		return newbmp;
	}
}

package com.example.screenshot;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

public class ScreenShot {
	// 获取指定Activity的截屏,保存到png文件
	public static Bitmap takeScreenShot(Activity activity) {
		// View是你需要截图的View
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap b1 = view.getDrawingCache();

		// 获取状态栏高度
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		Log.i("TAG", "" + statusBarHeight);

		// 获取屏幕长和高
		int width = activity.getWindowManager().getDefaultDisplay().getWidth();
		int height = activity.getWindowManager().getDefaultDisplay().getHeight();
		// 去掉状态栏
		Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);

		view.destroyDrawingCache();
		return b;
	}

	// 保存到sdcard
	public static void savePic(Bitmap b, String strFileName) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(strFileName);
			if (null != fos) {
				b.compress(Bitmap.CompressFormat.PNG, 90, fos);
				fos.flush();
				fos.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 程序入口
	public static void shoot(Activity a, String path) {

		ScreenShot.savePic(ScreenShot.takeScreenShot(a), path);
	}
}

 内容参考网上博客。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值