Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.24 KB | None | 0 0
  1. public static String[] cmdCreateVideo(int frame,String videoSource, String outPut){
  2. float duration_frame=1.0F;
  3. if (frame<= 20) {
  4. duration_frame = 1.0F;
  5. }else {
  6. duration_frame = 0.6F;
  7. }
  8. String str5 = "-framerate 1/" + duration_frame + " -start_number 0 -i " + videoSource + " -vcodec mpeg4 -q:v 3 -r 20 -vf scale=480x800 " +outPut;
  9. return str5.split(" ");
  10. }
  11.  
  12. import android.app.Activity;
  13. import android.app.AlertDialog;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.graphics.Bitmap;
  17. import android.os.AsyncTask;
  18. import android.os.Bundle;
  19. import android.os.Parcelable;
  20. import android.support.v4.view.PagerAdapter;
  21. import android.support.v4.view.ViewPager;
  22. import android.view.View;
  23. import android.view.ViewGroup;
  24. import android.widget.Button;
  25. import android.widget.ImageView;
  26. import android.widget.TextView;
  27. import android.widget.Toast;
  28.  
  29. import com.bumptech.glide.Glide;
  30. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  31. import com.bumptech.glide.request.target.Target;
  32. import com.hini.slideshow.R;
  33. import com.hini.slideshow.SlideApplication;
  34. import com.hini.slideshow.draw.SlideShow;
  35. import com.hini.slideshow.encoding.SlideEncoder;
  36.  
  37. import java.util.concurrent.ExecutionException;
  38.  
  39. /**
  40. * Created by PrakashSaurav.
  41. */
  42. public class EffectActivity extends Activity implements View.OnClickListener {
  43. private static final String TAG = "EffectActivity";
  44. private ViewPager mViewPager;
  45. private Button mBtnBitRate, mBtnTime, mBtnEffect, mBtnNext, mBtnPrev;
  46. private TextView mTvBitRate, mTvTime, mTvEffect;
  47. private Parcelable[] mParcelableUris;
  48.  
  49. @Override
  50. protected void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. setContentView(R.layout.activity_effect);
  53.  
  54. mParcelableUris = getIntent().getParcelableArrayExtra("parcelableUris");
  55. if (mParcelableUris == null) {
  56. Toast.makeText(getApplicationContext(), "There is no path info for the photo.", Toast.LENGTH_SHORT).show();
  57. return;
  58. }
  59. mViewPager = (ViewPager) findViewById(R.id.pager_image);
  60. mViewPager.setAdapter(new CustomPageAdapter());
  61.  
  62. mBtnBitRate = (Button) findViewById(R.id.btn_bgm);
  63. mTvBitRate = (TextView) findViewById(R.id.tv_bitrate);
  64.  
  65. mBtnTime = (Button) findViewById(R.id.btn_time);
  66. mTvTime = (TextView) findViewById(R.id.tv_time);
  67.  
  68. mBtnEffect = (Button) findViewById(R.id.btn_effect);
  69. mTvEffect = (TextView) findViewById(R.id.tv_effect);
  70.  
  71. mBtnNext = (Button) findViewById(R.id.btn_next);
  72. mBtnPrev = (Button) findViewById(R.id.btn_prev);
  73.  
  74. mBtnBitRate.setOnClickListener(this);
  75. mBtnTime.setOnClickListener(this);
  76. mBtnEffect.setOnClickListener(this);
  77. mBtnNext.setOnClickListener(this);
  78. mBtnPrev.setOnClickListener(this);
  79. }
  80.  
  81. int timeCheck = 0;
  82. int effectCheck = 0;
  83.  
  84. boolean isClicked;
  85.  
  86. @Override
  87. public void onClick(View v) {
  88. // BGM Setting
  89. if (v == mBtnBitRate) {
  90. isClicked = !isClicked;
  91. int bitRate;
  92. if (isClicked)
  93. bitRate = 2000 * 1024;
  94. else
  95. bitRate = 500 * 1024;
  96. SlideApplication.BIT_RATE = bitRate;
  97. mTvBitRate.setText(String.valueOf(bitRate / 1024) + "kbps");
  98. Toast.makeText(getApplicationContext(), SlideApplication.BIT_RATE / 1024 + "kbps", Toast.LENGTH_SHORT).show();
  99. }
  100. // Time Setting
  101. else if (v == mBtnTime) {
  102. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  103. final String[] items = {"2s", "3s", "5s", "10s"};
  104.  
  105. if (SlideApplication.SLIDE_TIME == 2) timeCheck = 0;
  106. else if (SlideApplication.SLIDE_TIME == 3) timeCheck = 1;
  107. else if (SlideApplication.SLIDE_TIME == 5) timeCheck = 2;
  108. else if (SlideApplication.SLIDE_TIME == 10) timeCheck = 3;
  109.  
  110. builder.setSingleChoiceItems(items, timeCheck, new DialogInterface.OnClickListener() {
  111. @Override
  112. public void onClick(DialogInterface dialog, int which) {
  113. timeCheck = which;
  114. Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
  115. }
  116. });
  117. builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
  118. @Override
  119. public void onClick(DialogInterface dialog, int which) {
  120. dialog.dismiss();
  121. }
  122. });
  123. builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
  124. @Override
  125. public void onClick(DialogInterface dialog, int which) {
  126. SlideApplication.SLIDE_TIME = Integer.parseInt(items[timeCheck].split("초")[0]);
  127. mTvTime.setText(Integer.toString(SlideApplication.SLIDE_TIME));
  128. }
  129. });
  130. AlertDialog dialog = builder.create();
  131. dialog.show();
  132. }
  133. // Effect Setting
  134. else if (v == mBtnEffect) {
  135. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  136. final String[] items = {"None", "FadeIn", "Rotate", "SlideIn"};
  137. if (SlideApplication.SLIDE_EFFECT == SlideShow.NONE) effectCheck = 0;
  138. else if (SlideApplication.SLIDE_EFFECT == SlideShow.FADE_IN) effectCheck = 1;
  139. else if (SlideApplication.SLIDE_EFFECT == SlideShow.ROTATE) effectCheck = 2;
  140. else if (SlideApplication.SLIDE_EFFECT == SlideShow.SLIDE_IN) effectCheck = 3;
  141.  
  142. builder.setSingleChoiceItems(items, effectCheck, new DialogInterface.OnClickListener() {
  143. @Override
  144. public void onClick(DialogInterface dialog, int which) {
  145. effectCheck = which;
  146. Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
  147. }
  148. });
  149. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  150. @Override
  151. public void onClick(DialogInterface dialog, int which) {
  152. dialog.dismiss();
  153. }
  154. });
  155. builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
  156. @Override
  157. public void onClick(DialogInterface dialog, int which) {
  158. SlideApplication.SLIDE_EFFECT = effectCheck;
  159. mTvEffect.setText(items[SlideApplication.SLIDE_EFFECT]);
  160. }
  161. });
  162. AlertDialog dialog = builder.create();
  163. dialog.show();
  164. }
  165. // Next
  166. else if (v == mBtnNext) {
  167. new BitmapChangerTask().execute();
  168. }
  169. // Prev
  170. else if (v == mBtnPrev) {
  171. finish();
  172. }
  173. }
  174.  
  175. @Override
  176. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  177. super.onActivityResult(requestCode, resultCode, data);
  178. if (requestCode == 1) {
  179. if (resultCode == RESULT_OK) {
  180. if (data != null && data.getStringExtra("bgm_path") != null) {
  181. SlideApplication.BGM_PATH = data.getStringExtra("bgm_path");
  182. }
  183. }
  184. }
  185. }
  186.  
  187. private class BitmapChangerTask extends AsyncTask<Void, Void, Void> {
  188.  
  189. @Override
  190. protected Void doInBackground(Void... params) {
  191. for (int i = 0; i < mParcelableUris.length; i++) {
  192. try {
  193. Bitmap bm = Glide.with(getApplicationContext()).load(mParcelableUris[i].toString())
  194. .asBitmap()
  195. .diskCacheStrategy(DiskCacheStrategy.ALL)
  196. .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
  197. .get();
  198. int width = bm.getWidth();
  199. int height = bm.getHeight();
  200.  
  201. // Land ( 1280 x 720 )
  202. if (width > height) {
  203. bm = Bitmap.createScaledBitmap(bm, SlideEncoder.WIDTH, ((SlideEncoder.WIDTH * height) / width), true);
  204. }
  205. // Port ( 720 x 1280 )
  206. else if (width < height) {
  207. bm = Bitmap.createScaledBitmap(bm, ((SlideEncoder.HEIGHT * width) / height), SlideEncoder.HEIGHT, true);
  208. }
  209. // Square ( 800 x 800 )
  210. else {
  211. bm = Bitmap.createScaledBitmap(bm, SlideEncoder.WIDTH, SlideEncoder.HEIGHT, true);
  212. }
  213. SlideApplication.bitmapList.add(bm);
  214. } catch (InterruptedException e) {
  215. e.printStackTrace();
  216. } catch (ExecutionException e) {
  217. e.printStackTrace();
  218. }
  219. }
  220. return null;
  221. }
  222.  
  223. @Override
  224. protected void onPostExecute(Void aVoid) {
  225. super.onPostExecute(aVoid);
  226. Intent i = new Intent(getApplicationContext(), EncodingActivity.class);
  227. startActivity(i);
  228. }
  229. }
  230.  
  231.  
  232. private class CustomPageAdapter extends PagerAdapter {
  233.  
  234. @Override
  235. public int getCount() {
  236. return mParcelableUris.length;
  237. }
  238.  
  239. @Override
  240. public int getItemPosition(Object object) {
  241. return POSITION_NONE;
  242. }
  243.  
  244.  
  245. @Override
  246. public Object instantiateItem(ViewGroup container, int position) {
  247. ImageView iv = new ImageView(getApplicationContext());
  248. Glide.with(getApplicationContext())
  249. .load(mParcelableUris[position].toString())
  250. .diskCacheStrategy(DiskCacheStrategy.ALL)
  251. .into(iv);
  252. try {
  253. container.addView(iv);
  254. } catch (IllegalStateException ise) {
  255. ise.printStackTrace();
  256. }
  257.  
  258. return iv;
  259. }
  260.  
  261. @Override
  262. public void destroyItem(ViewGroup container, int position, Object object) {
  263. container.removeView((View) object);
  264. }
  265.  
  266. @Override
  267. public boolean isViewFromObject(View view, Object object) {
  268. return view == object;
  269. }
  270. }
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. EffectActivity.xml
  283.  
  284. <?xml version="1.0" encoding="utf-8"?>
  285. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  286. android:layout_width="match_parent"
  287. android:layout_height="match_parent">
  288.  
  289. <android.support.v4.view.ViewPager
  290. android:id="@+id/pager_image"
  291. android:layout_width="match_parent"
  292. android:layout_height="350dp">
  293. </android.support.v4.view.ViewPager>
  294.  
  295. <Button
  296. android:id="@+id/btn_bgm"
  297. android:layout_width="wrap_content"
  298. android:layout_height="wrap_content"
  299. android:layout_below="@+id/pager_image"
  300. android:layout_marginStart="80dp"
  301. android:layout_marginTop="30dp"
  302. android:text="BitRate"/>
  303.  
  304. <TextView
  305. android:id="@+id/tv_bitrate"
  306. android:layout_width="wrap_content"
  307. android:layout_height="wrap_content"
  308. android:layout_alignBaseline="@+id/btn_bgm"
  309. android:layout_alignParentEnd="true"
  310. android:layout_below="@+id/pager_image"
  311. android:layout_marginEnd="80dp"
  312. android:singleLine="true"
  313. android:ellipsize="marquee"
  314. android:text="500Kbps"
  315. android:textSize="20dp"/>
  316.  
  317. <Button
  318. android:id="@+id/btn_time"
  319. android:layout_width="wrap_content"
  320. android:layout_height="wrap_content"
  321. android:layout_below="@+id/btn_bgm"
  322. android:layout_marginStart="80dp"
  323. android:text="time"/>
  324.  
  325.  
  326. <TextView
  327. android:id="@+id/tv_time"
  328. android:layout_width="wrap_content"
  329. android:layout_height="wrap_content"
  330. android:layout_alignBaseline="@+id/btn_time"
  331. android:layout_alignParentEnd="true"
  332. android:layout_below="@+id/pager_image"
  333. android:layout_marginEnd="80dp"
  334. android:text="2s"
  335. android:textSize="20dp"/>
  336.  
  337.  
  338. <Button
  339. android:id="@+id/btn_effect"
  340. android:layout_width="wrap_content"
  341. android:layout_height="wrap_content"
  342. android:layout_below="@+id/btn_time"
  343. android:layout_marginStart="80dp"
  344. android:text="EFFECT"/>
  345.  
  346.  
  347. <TextView
  348. android:id="@+id/tv_effect"
  349. android:layout_width="wrap_content"
  350. android:layout_height="wrap_content"
  351. android:layout_alignBaseline="@+id/btn_effect"
  352. android:layout_alignParentEnd="true"
  353. android:layout_below="@+id/pager_image"
  354. android:layout_marginEnd="80dp"
  355. android:text="none"
  356. android:textSize="20dp"/>
  357.  
  358. <Button
  359. android:id="@+id/btn_prev"
  360. android:layout_width="wrap_content"
  361. android:layout_height="wrap_content"
  362. android:layout_alignParentBottom="true"
  363. android:layout_alignParentStart="true"
  364. android:text="Previous"/>
  365.  
  366. <Button
  367. android:id="@+id/btn_next"
  368. android:layout_width="wrap_content"
  369. android:layout_height="wrap_content"
  370. android:layout_alignParentBottom="true"
  371. android:layout_alignParentEnd="true"
  372. android:text="NEXT"/>
  373. </RelativeLayout>
  374.  
  375. import android.app.Activity;
  376. import android.app.ProgressDialog;
  377. import android.content.Intent;
  378. import android.graphics.Bitmap;
  379. import android.os.AsyncTask;
  380. import android.os.Bundle;
  381. import android.os.Environment;
  382. import android.util.Log;
  383.  
  384. import com.hini.slideshow.SlideApplication;
  385. import com.hini.slideshow.draw.SlideShow;
  386. import com.hini.slideshow.encoding.SlideEncoder;
  387.  
  388. import java.io.File;
  389. import java.io.IOException;
  390.  
  391.  
  392. public class EncodingActivity extends Activity {
  393. @Override
  394. protected void onCreate(Bundle savedInstanceState) {
  395. super.onCreate(savedInstanceState);
  396.  
  397. new EncodingTask().execute();
  398. }
  399.  
  400. class EncodingTask extends AsyncTask<Void, String, Void> {
  401. File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
  402. + "/test_" + SlideApplication.BIT_RATE / 1024 + "_" + SlideApplication.SLIDE_EFFECT + ".mp4");
  403. ProgressDialog dialog;
  404.  
  405. @Override
  406. protected void onPreExecute() {
  407. super.onPreExecute();
  408. dialog = new ProgressDialog(EncodingActivity.this);
  409. dialog.setTitle("Generating Video..");
  410. dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  411. dialog.setCancelable(false);
  412. dialog.show();
  413. }
  414.  
  415. @Override
  416. protected Void doInBackground(Void... params) {
  417. long startTime = System.currentTimeMillis();
  418. if (f.exists()) f.delete();
  419. SlideEncoder slideEncoder = new SlideEncoder();
  420.  
  421. try {
  422. slideEncoder.prepareEncoder(f);
  423. Bitmap prevBm = null;
  424. dialog.setMax(SlideApplication.bitmapList.size());
  425. for (int idx = 0; idx < SlideApplication.bitmapList.size(); idx++) {
  426. publishProgress(String.valueOf(idx + 1));
  427. SlideShow.init();
  428.  
  429. if (idx > 0) prevBm = SlideApplication.bitmapList.get(idx - 1);
  430. Bitmap curBm = SlideApplication.bitmapList.get(idx);
  431. for (int i = 0; i < (SlideApplication.FRAME_PER_SEC * SlideApplication.SLIDE_TIME); i++) {
  432. // Drain any data from the encoder into the muxer.
  433. slideEncoder.drainEncoder(false);
  434.  
  435. // Generate a frame and submit it.
  436. slideEncoder.generateFrame(prevBm, curBm);
  437. }
  438. }
  439. slideEncoder.drainEncoder(true);
  440. } catch (IOException e) {
  441. e.printStackTrace();
  442. } finally {
  443. slideEncoder.releaseEncoder();
  444. }
  445. Log.e("TAG", "total time : " + (System.currentTimeMillis() - startTime));
  446. return null;
  447. }
  448.  
  449. @Override
  450. protected void onProgressUpdate(String... values) {
  451. super.onProgressUpdate(values);
  452. dialog.setProgress(Integer.parseInt(values[0]));
  453. }
  454.  
  455. @Override
  456. protected void onPostExecute(Void aVoid) {
  457. super.onPostExecute(aVoid);
  458. if (dialog.isShowing()) dialog.dismiss();
  459. for (Bitmap bm : SlideApplication.bitmapList) bm.recycle();
  460. SlideApplication.bitmapList.clear();
  461. Intent i = new Intent(EncodingActivity.this, MainActivity.class);
  462. i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  463. startActivity(i);
  464. }
  465. }
  466.  
  467.  
  468.  
  469.  
  470.  
  471. *****SlideEncoder.java*****
  472.  
  473.  
  474.  
  475. import android.graphics.Bitmap;
  476. import android.graphics.Canvas;
  477. import android.media.MediaCodec;
  478. import android.media.MediaCodecInfo;
  479. import android.media.MediaFormat;
  480. import android.media.MediaMuxer;
  481. import android.view.Surface;
  482.  
  483. import com.hini.slideshow.SlideApplication;
  484. import com.hini.slideshow.draw.SlideShow;
  485.  
  486. import java.io.File;
  487. import java.io.IOException;
  488. import java.nio.ByteBuffer;
  489.  
  490.  
  491. public class SlideEncoder {
  492. private static final String MIME_TYPE = MediaFormat.MIMETYPE_VIDEO_AVC;
  493. public static final int WIDTH = 800;
  494. public static final int HEIGHT = 800;
  495. private static final int IFRAME_INTERVAL = 3;
  496. private MediaCodec.BufferInfo mBufferInfo;
  497. private MediaCodec mEncoder;
  498. private Surface mInputSurface;
  499. private MediaMuxer mMuxer;
  500. private int mTrackIndex;
  501. private boolean mMuxerStarted;
  502. private long mFakePts;
  503.  
  504. void prepareEncoder(File outputFile) throws IOException {
  505. mBufferInfo = new MediaCodec.BufferInfo();
  506.  
  507. MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
  508.  
  509. // Set some properties. Failing to specify some of these can cause the MediaCodec
  510. // configure() call to throw an unhelpful exception.
  511. format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
  512. MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
  513. format.setInteger(MediaFormat.KEY_BIT_RATE, SlideApplication.BIT_RATE);
  514. format.setInteger(MediaFormat.KEY_FRAME_RATE, SlideApplication.FRAME_PER_SEC);
  515. format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
  516.  
  517. // Create a MediaCodec encoder, and configure it with our format. Get a Surface
  518. // we can use for input and wrap it with a class that handles the EGL work.
  519. mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
  520. mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  521. mInputSurface = mEncoder.createInputSurface();
  522. mEncoder.start();
  523.  
  524. // Create a MediaMuxer. We can't add the video track and start() the muxer here,
  525. // because our MediaFormat doesn't have the Magic Goodies. These can only be
  526. // obtained from the encoder after it has started processing data.
  527. //
  528. // We're not actually interested in multiplexing audio. We just want to convert
  529. // the raw H.264 elementary stream we get from MediaCodec into a .mp4 file.
  530. mMuxer = new MediaMuxer(outputFile.toString(),
  531. MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
  532.  
  533. mTrackIndex = -1;
  534. mMuxerStarted = false;
  535. }
  536.  
  537. /**
  538. * MediaCodec, InputSurface, MediaMuxer 해제
  539. */
  540. public void releaseEncoder() {
  541. if (mEncoder != null) {
  542. mEncoder.stop();
  543. mEncoder.release();
  544. mEncoder = null;
  545. }
  546. if (mInputSurface != null) {
  547. mInputSurface.release();
  548. mInputSurface = null;
  549. }
  550. if (mMuxer != null) {
  551. mMuxer.stop();
  552. mMuxer.release();
  553. mMuxer = null;
  554. }
  555. }
  556.  
  557. /**
  558. * Extracts all pending data from the encoder.
  559. * <p/>
  560. * If endOfStream is not set, this returns when there is no more data to drain. If it
  561. * is set, we send EOS to the encoder, and then iterate until we see EOS on the output.
  562. * Calling this with endOfStream set should be done once, right before stopping the muxer.
  563. */
  564. public void drainEncoder(boolean endOfStream) {
  565. final int TIMEOUT_USEC = 10000;
  566.  
  567. if (endOfStream) {
  568. mEncoder.signalEndOfInputStream();
  569. }
  570.  
  571. ByteBuffer[] encoderOutputBuffers = mEncoder.getOutputBuffers();
  572. while (true) {
  573. int encoderStatus = mEncoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC);
  574. if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
  575. // no output available yet
  576. if (!endOfStream) {
  577. break; // out of while
  578. } else {
  579. }
  580. } else if (encoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
  581. // not expected for an encoder
  582. encoderOutputBuffers = mEncoder.getOutputBuffers();
  583. } else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
  584. // should happen before receiving buffers, and should only happen once
  585. if (mMuxerStarted) {
  586. throw new RuntimeException("format changed twice");
  587. }
  588. MediaFormat newFormat = mEncoder.getOutputFormat();
  589.  
  590. // now that we have the Magic Goodies, start the muxer
  591. mTrackIndex = mMuxer.addTrack(newFormat);
  592. mMuxer.start();
  593. mMuxerStarted = true;
  594. } else if (encoderStatus < 0) {
  595. // let's ignore it
  596. } else {
  597. ByteBuffer encodedData = encoderOutputBuffers[encoderStatus];
  598. if (encodedData == null) {
  599. throw new RuntimeException("encoderOutputBuffer " + encoderStatus +
  600. " was null");
  601. }
  602.  
  603. if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
  604. // The codec config data was pulled out and fed to the muxer when we got
  605. // the INFO_OUTPUT_FORMAT_CHANGED status. Ignore it.
  606. mBufferInfo.size = 0;
  607. }
  608.  
  609. if (mBufferInfo.size != 0) {
  610. if (!mMuxerStarted) {
  611. throw new RuntimeException("muxer hasn't started");
  612. }
  613.  
  614. // adjust the ByteBuffer values to match BufferInfo
  615. encodedData.position(mBufferInfo.offset);
  616. encodedData.limit(mBufferInfo.offset + mBufferInfo.size);
  617. mBufferInfo.presentationTimeUs = mFakePts;
  618. long timeStampLength = 1000000L / SlideApplication.FRAME_PER_SEC;
  619. mFakePts += timeStampLength;
  620.  
  621. mMuxer.writeSampleData(mTrackIndex, encodedData, mBufferInfo);
  622. }
  623.  
  624. mEncoder.releaseOutputBuffer(encoderStatus, false);
  625.  
  626. if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
  627. if (!endOfStream) {
  628. } else {
  629. }
  630. break; // out of while
  631. }
  632. }
  633. }
  634. }
  635.  
  636. /**
  637. * Generates a frame, writing to the Surface via the "software" API (lock/unlock).
  638. * <p/>
  639. * There's no way to set the time stamp.
  640. */
  641. public void generateFrame(Bitmap prevBm, Bitmap curBm) {
  642. Canvas canvas = mInputSurface.lockCanvas(null);
  643. try {
  644. SlideShow.draw(canvas, prevBm, curBm);
  645. } finally {
  646. mInputSurface.unlockCanvasAndPost(canvas);
  647. }
  648. }
  649. }
  650.  
  651. import android.graphics.Bitmap;
  652. import android.graphics.Canvas;
  653. import android.graphics.Color;
  654. import android.graphics.Matrix;
  655. import android.graphics.Paint;
  656.  
  657. import com.appwallet.slideshow.SlideApplication;
  658. import com.appwallet.slideshow.encoding.SlideEncoder;
  659.  
  660.  
  661. public class SlideShow {
  662. // effect
  663. public static final int NONE = 0;
  664. public static final int FADE_IN = 1;
  665. public static final int ROTATE = 2;
  666. public static final int SLIDE_IN = 3;
  667.  
  668. // 위치
  669. private static int curStartX;
  670. private static int curStartY;
  671. private static int prevStartX;
  672. private static int prevStartY;
  673.  
  674. //Variables used for effect
  675. private static float in_alpha = 0f;
  676. private static float rotate = 0;
  677. private static int slideX = SlideEncoder.WIDTH;
  678. private static int slideCount = 1;
  679. private static float out_alpha = 255f;
  680.  
  681. /**
  682. *
  683. * @param canvas canvas
  684. * @param prevBm Background Bitmap
  685. * @param curBm Foregound Bitmap
  686. */
  687. public static void draw(Canvas canvas, Bitmap prevBm, Bitmap curBm) {
  688. if (canvas == null || curBm == null) return;
  689.  
  690. setLocation(prevBm, curBm);
  691.  
  692. if (SlideApplication.SLIDE_EFFECT == ROTATE && prevBm == null)
  693. canvas.drawColor(Color.BLACK);
  694.  
  695. if (prevBm != null) drawFadeOut(canvas, prevBm);
  696. switch (SlideApplication.SLIDE_EFFECT) {
  697. case NONE:
  698. drawNone(canvas, curBm);
  699. break;
  700. case FADE_IN:
  701. drawFadeIn(canvas, curBm);
  702. break;
  703. case ROTATE:
  704. drawRotate(canvas, curBm);
  705. break;
  706. case SLIDE_IN:
  707. drawSlideIn(canvas, curBm);
  708. break;
  709. default:
  710. throw new IllegalStateException("unexpected state");
  711. }
  712. }
  713.  
  714. /*
  715. *Adjust position according to figure
  716. * @param prevBm Background Bitmap
  717. * @param curBm Foreground Bitmap
  718. */
  719. private static void setLocation(Bitmap prevBm, Bitmap curBm) {
  720. if (curBm != null) {
  721. int cWidth = curBm.getWidth();
  722. int cHeight = curBm.getHeight();
  723.  
  724. if (cWidth > cHeight) {
  725. curStartX = 0;
  726. curStartY = (SlideEncoder.HEIGHT - cHeight) / 2;
  727. } else if (cHeight > cWidth) {
  728. curStartX = (SlideEncoder.WIDTH - cWidth) / 2;
  729. curStartY = 0;
  730. } else {
  731. curStartX = 0;
  732. curStartY = 0;
  733. }
  734. }
  735.  
  736. if (prevBm != null) {
  737. int pWidth = prevBm.getWidth();
  738. int pHeight = prevBm.getHeight();
  739. if (pWidth > pHeight) {
  740. prevStartX = 0;
  741. prevStartY = (SlideEncoder.HEIGHT - pHeight) / 2;
  742. } else if (pHeight > pWidth) {
  743. prevStartX = (SlideEncoder.WIDTH - pWidth) / 2;
  744. prevStartY = 0;
  745. } else {
  746. prevStartX = 0;
  747. prevStartY = 0;
  748. }
  749. }
  750. }
  751.  
  752. /**
  753. */
  754. public static void init() {
  755. in_alpha = 0f;
  756. out_alpha = 255f;
  757. rotate = 0f;
  758. slideX = 800;
  759. slideCount = 1;
  760. curStartX = 0;
  761. curStartY = 0;
  762. prevStartX = 0;
  763. prevStartY = 0;
  764. }
  765.  
  766. /**
  767. * drawNone
  768. *
  769. * @param c canvas
  770. * @param bm bitmap
  771. */
  772. private static void drawNone(Canvas c, Bitmap bm) {
  773. c.drawBitmap(bm, curStartX, curStartY, null);
  774. }
  775.  
  776.  
  777. * Fade in effect *
  778.  
  779.  
  780. private static void drawFadeIn(Canvas c, Bitmap bm) {
  781. Paint p = new Paint();
  782. int ratio = (int) Math.ceil(255 / SlideApplication.FRAME_PER_SEC);
  783. in_alpha += ratio;
  784. if (in_alpha > 255f) in_alpha = 255f;
  785. p.setAlpha((int) in_alpha);
  786. c.drawBitmap(bm, curStartX, curStartY, p);
  787. }
  788.  
  789.  
  790. Rotate effect
  791.  
  792. private static void drawRotate(Canvas c, Bitmap bm) {
  793. Matrix matrix = new Matrix();
  794. matrix.preTranslate(curStartX, curStartY);
  795. float ratio = 360 / SlideApplication.FRAME_PER_SEC;
  796. rotate += Math.ceil(ratio);
  797. if (rotate > 360) rotate = 360;
  798. matrix.postRotate(rotate, SlideEncoder.WIDTH / 2, SlideEncoder.HEIGHT / 2);
  799. c.drawBitmap(bm, matrix, null);
  800. }
  801.  
  802. /**
  803. * drawSlideIn drawSlideIn
  804. *
  805. * @param c canvas
  806. * @param bm bitmap
  807. */
  808.  
  809. private static void drawSlideIn(Canvas c, Bitmap bm) {
  810. Matrix matrix = new Matrix();
  811. int ratio = 1;
  812. if (slideCount < 30) ratio = (int) Math.pow(slideCount++, 1.4);
  813. slideX -= ratio;
  814. if (slideX < curStartX) slideX = curStartX;
  815. matrix.setTranslate(slideX, curStartY);
  816. c.drawBitmap(bm, matrix, null);
  817.  
  818. }
  819.  
  820. /**
  821. *
  822. Fade-out effect (applies only to pictures that are drawn behind)
  823. *
  824. * @param c canvas
  825. * @param bm bitmap
  826. */
  827.  
  828. private static void drawFadeOut(Canvas c, Bitmap bm) {
  829. c.drawColor(Color.BLACK);
  830. Paint p = new Paint();
  831. int ratio = (int) Math.ceil(255 / SlideApplication.FRAME_PER_SEC);
  832. out_alpha -= ratio;
  833. if (out_alpha < 0f) out_alpha = 0f;
  834. p.setAlpha((int) out_alpha);
  835. c.drawBitmap(bm, prevStartX, prevStartY, p);
  836. }
  837. }
  838.  
  839. import android.app.Application;
  840. import android.graphics.Bitmap;
  841. import android.net.Uri;
  842. import android.os.Parcelable;
  843. import android.view.View;
  844.  
  845. import com.appwallet.slideshow.draw.SlideShow;
  846.  
  847. import java.util.ArrayList;
  848.  
  849. public class SlideApplication extends Application {
  850. public static String BGM_PATH = "";
  851. public static int SLIDE_TIME = 2;
  852. public static int SLIDE_EFFECT = SlideShow.NONE;
  853. public static int BIT_RATE = 2000 * 1024;
  854. public static int FRAME_PER_SEC = 30;
  855. public static ArrayList<Bitmap> bitmapList = new ArrayList<>();
  856. public static int overlayEffect=0;
  857. public static Parcelable[] images;
  858. public static String deleteFolderPath;
  859.  
  860.  
  861. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement