Skip to content

Commit

Permalink
在屏幕上告知保存路径,整理格式
Browse files Browse the repository at this point in the history
  • Loading branch information
zhantong committed Sep 8, 2016
1 parent 8a07ec3 commit a3553b9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
71 changes: 39 additions & 32 deletions app/src/main/java/com/polarxiong/videotoimages/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
Expand All @@ -16,17 +15,19 @@
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements VideoToFrames.Callback{
private static final int REQUEST_CODE_GET_FILE_PATH=1;
public class MainActivity extends Activity implements VideoToFrames.Callback {
private static final int REQUEST_CODE_GET_FILE_PATH = 1;
private OutputImageFormat outputImageFormat;
private MainActivity self=this;
private MainActivity self = this;
private String outputDir;

final Handler handler=new Handler(){
public void handleMessage(Message msg){
String str=(String)msg.obj;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String str = (String) msg.obj;
updateInfo(str);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -46,25 +47,26 @@ public void onClick(View v) {
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editTextOutputFolder=(EditText)findViewById(R.id.folder_created);
String outputDir= Environment.getExternalStorageDirectory()+"/"+editTextOutputFolder.getText().toString();
EditText editTextInputFilePath=(EditText)findViewById(R.id.file_path_input);
String inputFilePath= editTextInputFilePath.getText().toString();
EditText editTextOutputFolder = (EditText) findViewById(R.id.folder_created);
outputDir = Environment.getExternalStorageDirectory() + "/" + editTextOutputFolder.getText().toString();
EditText editTextInputFilePath = (EditText) findViewById(R.id.file_path_input);
String inputFilePath = editTextInputFilePath.getText().toString();
VideoToFrames videoToFrames = new VideoToFrames();
videoToFrames.setCallback(self);
try {
videoToFrames.setSaveFrames(outputDir, outputImageFormat);
updateInfo("运行中...");
videoToFrames.decode(inputFilePath);
}catch (Throwable t){
} catch (Throwable t) {
t.printStackTrace();
}
}
});
}
private void initImageFormatSpinner(){
Spinner barcodeFormatSpinner=(Spinner)findViewById(R.id.image_format);
ArrayAdapter<OutputImageFormat> adapter=new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, OutputImageFormat.values());

private void initImageFormatSpinner() {
Spinner barcodeFormatSpinner = (Spinner) findViewById(R.id.image_format);
ArrayAdapter<OutputImageFormat> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, OutputImageFormat.values());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
barcodeFormatSpinner.setAdapter(adapter);
barcodeFormatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
Expand All @@ -79,44 +81,49 @@ public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void getFilePath(int requestCode){
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);

private void getFilePath(int requestCode) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
if(intent.resolveActivity(getPackageManager())!=null){
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Select a File"), requestCode);
}else{
} else {
new AlertDialog.Builder(this).setTitle("未找到文件管理器")
.setMessage("请安装文件管理器以选择文件")
.setPositiveButton("确定",null)
.setPositiveButton("确定", null)
.show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
int id=0;
switch (requestCode){

public void onActivityResult(int requestCode, int resultCode, Intent data) {
int id = 0;
switch (requestCode) {
case REQUEST_CODE_GET_FILE_PATH:
id=R.id.file_path_input;
id = R.id.file_path_input;
break;
}
if (resultCode == Activity.RESULT_OK) {
EditText editText = (EditText) findViewById(id);
String curFileName=data.getData().getPath();
String curFileName = data.getData().getPath();
editText.setText(curFileName);
}
}
private void updateInfo(String info){

private void updateInfo(String info) {
TextView textView = (TextView) findViewById(R.id.info);
textView.setText(info);
}
public void onDecodeFrame(int index){
Message msg=handler.obtainMessage();
msg.obj="运行中...第"+index+"帧";

public void onDecodeFrame(int index) {
Message msg = handler.obtainMessage();
msg.obj = "运行中...第" + index + "帧";
handler.sendMessage(msg);
}
public void onFinishDecode(){
Message msg=handler.obtainMessage();
msg.obj="完成!";

public void onFinishDecode() {
Message msg = handler.obtainMessage();
msg.obj = "完成!所有图片已存储到" + outputDir;
handler.sendMessage(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public enum OutputImageFormat {
NV21("NV21"),
JPEG("JPEG");
private String friendlyName;
private OutputImageFormat(String friendlyName){
this.friendlyName=friendlyName;

private OutputImageFormat(String friendlyName) {
this.friendlyName = friendlyName;
}
public String toString(){

public String toString() {
return friendlyName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public class VideoToFrames implements Runnable {

private Callback callback;

public interface Callback{
public interface Callback {
void onFinishDecode();

void onDecodeFrame(int index);
}
public void setCallback(Callback callback){
this.callback=callback;

public void setCallback(Callback callback) {
this.callback = callback;
}

public void setEnqueue(LinkedBlockingQueue<byte[]> queue) {
mQueue = queue;
}
Expand Down Expand Up @@ -173,7 +176,7 @@ private void decodeFramesToImage(MediaCodec decoder, MediaExtractor extractor, M
boolean doRender = (info.size != 0);
if (doRender) {
outputFrameCount++;
if(callback!=null){
if (callback != null) {
callback.onDecodeFrame(outputFrameCount);
}
Image image = decoder.getOutputImage(outputBufferId);
Expand Down Expand Up @@ -212,7 +215,7 @@ private void decodeFramesToImage(MediaCodec decoder, MediaExtractor extractor, M
}
}
}
if(callback!=null) {
if (callback != null) {
callback.onFinishDecode();
}
}
Expand Down

0 comments on commit a3553b9

Please sign in to comment.