diff --git a/app/src/main/java/com/polarxiong/videotoimages/MainActivity.java b/app/src/main/java/com/polarxiong/videotoimages/MainActivity.java index 41521b4..91dd681 100644 --- a/app/src/main/java/com/polarxiong/videotoimages/MainActivity.java +++ b/app/src/main/java/com/polarxiong/videotoimages/MainActivity.java @@ -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; @@ -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); @@ -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 adapter=new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, OutputImageFormat.values()); + + private void initImageFormatSpinner() { + Spinner barcodeFormatSpinner = (Spinner) findViewById(R.id.image_format); + ArrayAdapter 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() { @@ -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); } } diff --git a/app/src/main/java/com/polarxiong/videotoimages/OutputImageFormat.java b/app/src/main/java/com/polarxiong/videotoimages/OutputImageFormat.java index 90b643b..934d7ed 100644 --- a/app/src/main/java/com/polarxiong/videotoimages/OutputImageFormat.java +++ b/app/src/main/java/com/polarxiong/videotoimages/OutputImageFormat.java @@ -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; } } diff --git a/app/src/main/java/com/polarxiong/videotoimages/VideoToFrames.java b/app/src/main/java/com/polarxiong/videotoimages/VideoToFrames.java index b9b2fa4..efab151 100644 --- a/app/src/main/java/com/polarxiong/videotoimages/VideoToFrames.java +++ b/app/src/main/java/com/polarxiong/videotoimages/VideoToFrames.java @@ -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 queue) { mQueue = queue; } @@ -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); @@ -212,7 +215,7 @@ private void decodeFramesToImage(MediaCodec decoder, MediaExtractor extractor, M } } } - if(callback!=null) { + if (callback != null) { callback.onFinishDecode(); } }