我想使用手机的相机录制视频,抓取并显示存储视频中的一帧。视频路径使用“sharedData.setValue(a);”存储在全局变量中。目前,我的应用程序可以运行,录制视频和保存。但是,我无法从视频中捕获帧(下面的代码)。申请刚刚关闭。
捕获并保存视频代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera2_video_image);
createVideoFolder();
createImageFolder();
mChronometer = (Chronometer) findViewById(R.id.chronometer);
mTextureView = (TextureView) findViewById(R.id.textureView);
mStillImageButton = (ImageButton) findViewById(R.id.cameraImageButton2);
mStillImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!(mIsTimelapse || mIsRecording)) {
checkWriteStoragePermission();
}
lockFocus();
}
});
Button btnProcess = (Button) findViewById(R.id.btnProcess);
btnProcess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Camera2VideoImageActivity.this, FrameDisplay.class);
startActivity(intent);
}
});
mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton);
mRecordImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIsRecording || mIsTimelapse) {
mChronometer.stop();
mChronometer.setVisibility(View.INVISIBLE);
mIsRecording = false;
mIsTimelapse = false;
mRecordImageButton.setImageResource(R.mipmap.btn_video_online);
// Starting the preview prior to stopping recording which should hopefully
// resolve issues being seen in Samsung devices.
startPreview();
mMediaRecorder.stop();
mMediaRecorder.reset();
Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mVideoFileName)));
String a = mediaStoreUpdateIntent.getDataString();
Log.e("Path Saved: ", a);
sharedData.setValue(a);
sendBroadcast(mediaStoreUpdateIntent);
} else {
mIsRecording = true;
mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
checkWriteStoragePermission();
}
}
});
mRecordImageButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mIsTimelapse =true;
mRecordImageButton.setImageResource(R.mipmap.btn_timelapse);
checkWriteStoragePermission();
return true;
}
});
}
全局变量代码:
public class Globals {
private static Globals instance = new Globals();
// Getter-Setters
public static Globals getInstance() {
return instance;
}
public static void setInstance(Globals instance) {
Globals.instance = instance;
}
private String notification_index;
private Globals() {
}
public String getValue() {
return notification_index;
}
public void setValue(String notification_index) {
this.notification_index = notification_index;
}
}
显示帧位图代码:
public class FrameDisplay extends AppCompatActivity {
Globals sharedData = Globals.getInstance();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_frame);
String n = sharedData.getValue();
ImageView imgView1 = (ImageView) findViewById(R.id.imgView);
TextView textView = (TextView) findViewById(R.id.textView);
Log.e("Path recieved: ",n);
try{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// Set data source to retriever.
// From your code, you might want to use your 'String path' here.
retriever.setDataSource(n);
// Get a frame in Bitmap by specifying time.
// Be aware that the parameter must be in "microseconds", not milliseconds.
Bitmap bitmap = retriever.getFrameAtTime(10000);
imgView1.setImageBitmap(bitmap);
textView.setEnabled(false);
}catch(Exception e){
e.printStackTrace();
}
imgView1.setBackgroundResource(R.mipmap.btn_camera);
textView.setEnabled(true);
}
}