我想通过在两个图像之间应用过渡动画来从图像列表制作动画视频.我在SO上发现了很多类似的问题,
Android Screen capturing or make video from images
Android- How to make video using set of images from sd card?
所有类似的SO问题建议使用动画,但我们如何将动画图像存储到视频文件?是否有任何Android库支持此工具制作图像视频?
任何帮助表示赞赏…… !!!
提前致谢
解决方法
Android不支持AWT的BufferedBitmap和AWTUtil,也就是Java SE.目前,SequenceEncoder的解决方案已集成到jcodec的Android版本中.您可以从包org.jcodec.api.SequenceEncoder中使用它.
以下是使用jcodec从一系列位图生成MP4文件的解决方案:
try { File file = this.GetSDPathToFile("","output.mp4"); SequenceEncoder encoder = new SequenceEncoder(file); // only 5 frames in total for (int i = 1; i <= 5; i++) { // getting bitmap from drawable path int bitmapResId = this.getResources().getIdentifier("image" + i,"drawable",this.getPackageName()); Bitmap bitmap = this.getBitmapFromResources(this.getResources(),bitmapResId); encoder.encodeNativeFrame(this.fromBitmap(bitmap)); } encoder.finish(); } catch (IOException e) { e.printStackTrace(); } // get full SD path File GetSDPathToFile(String filePatho,String fileName) { File extBaseDir = Environment.getExternalStorageDirectory(); if (filePatho == null || filePatho.length() == 0 || filePatho.charAt(0) != '/') filePatho = "/" + filePatho; makeDirectory(filePatho); File file = new File(extBaseDir.getAbsoluteFile() + filePatho); return new File(file.getAbsolutePath() + "/" + fileName);// file; } // convert from Bitmap to Picture (jcodec native structure) public Picture fromBitmap(Bitmap src) { Picture dst = Picture.create((int)src.getWidth(),(int)src.getHeight(),ColorSpace.RGB); fromBitmap(src,dst); return dst; } public void fromBitmap(Bitmap src,Picture dst) { int[] dstData = dst.getPlaneData(0); int[] packed = new int[src.getWidth() * src.getHeight()]; src.getPixels(packed,src.getWidth(),src.getHeight()); for (int i = 0,srcOff = 0,dstOff = 0; i < src.getHeight(); i++) { for (int j = 0; j < src.getWidth(); j++,srcOff++,dstOff += 3) { int rgb = packed[srcOff]; dstData[dstOff] = (rgb >> 16) & 0xff; dstData[dstOff + 1] = (rgb >> 8) & 0xff; dstData[dstOff + 2] = rgb & 0xff; } } }
如果您需要更改fps,可以自定义SequenceEncoder.