如何从Android中的图像数组创建视频()

【如何从Android中的图像数组创建视频()】千磨万击还坚劲,任尔东西南北风。这篇文章主要讲述如何从Android中的图像数组创建视频?相关的知识,希望能为你提供帮助。
我想调用一个函数并从图像列表中构建一个视频,然后将其本地保存在设备上:

public void CreateAndSaveVideoFile(List< Bitmap> MyBitmapArray) { // .. }

试验:
  • 在java/xuggle - encode array of images into a movie之后,答案中的链接是一个死链接
  • 在How to encode images into a video file in Java through programming?之后,接受的答案中建议的库不支持android。
  • 上面的下一个答案有针对Android用户的方法,但我不清楚该功能的输入和输出(他在哪里提供图像?他在哪里获得视频?) - 我留下了一个问题评论
  • 上面的下一个答案提供了一个完整的类,但是要包含的所需库有一个损坏的文件(当我尝试从提供的链接下载它时) - 我留下了一个问题评论
  • 在Java: How do I create a movie from an array of images?之后,顶部答案中建议的库使用了我不熟悉的命令,我甚至不知道如何使用它们。喜欢:
从当前目录中的所有JPEG文件创建MPEG-4文件:
mencoder mf://*.jpg -mf w=800:h=600:fps=25:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi
我不知道如何在java / Android项目中使用上述内容。
任何人都可以帮助指导我或/并为我提供一个方法来完成我的任务吗?提前致谢。
答案您可以使用jcodec SequenceEncoder将图像序列转换为MP4文件。
示例代码:
import org.jcodec.api.awt.SequenceEncoder; ... SequenceEncoder enc = new SequenceEncoder(new File("filename")); // GOP size will be supported in 0.2 // enc.getEncoder().setKeyInterval(25); for(...) { BufferedImage image = ... // Obtain an image to encode enc.encodeImage(image); } enc.finish();

它是一个java库,所以很容易将它导入到Android项目中,你不必像ffmpeg那样使用NDK。
有关示例代码和下载,请参阅http://jcodec.org/。
另一答案使用JCodec
public static void main(String[] args) throws IOException { SequenceEncoder encoder = new SequenceEncoder(new File("video.mp4")); for (int i = 1; i < 100; i++) { BufferedImage bi = ImageIO.read(new File(String.format("img%08d.png", i))); encoder.encodeImage(bi); } encoder.finish(); }

现在要将Bitmap转换为BufferedImage,您可以使用此类:
import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.DataBufferInt; import java.io.IOException; import java.io.InputStream; /** * Utility class for loading windows bitmap files * < p> * Based on code from author Abdul Bezrati and Pepijn Van Eeckhoudt */ public class BitmapLoader {/** * Static method to load a bitmap file based on the filename passed in. * Based on the bit count, this method will either call the 8 or 24 bit * bitmap reader methods * * @param file The name of the bitmap file to read * @throws IOException * @return A BufferedImage of the bitmap */ public static BufferedImage loadBitmap(String file) throws IOException { BufferedImage image; InputStream input = null; try { input = ResourceRetriever.getResourceAsStream(file); int bitmapFileHeaderLength = 14; int bitmapInfoHeaderLength = 40; byte bitmapFileHeader[] = new byte[bitmapFileHeaderLength]; byte bitmapInfoHeader[] = new byte[bitmapInfoHeaderLength]; input.read(bitmapFileHeader, 0, bitmapFileHeaderLength); input.read(bitmapInfoHeader, 0, bitmapInfoHeaderLength); int nSize = bytesToInt(bitmapFileHeader, 2); int nWidth = bytesToInt(bitmapInfoHeader, 4); int nHeight = bytesToInt(bitmapInfoHeader, 8); int nBiSize = bytesToInt(bitmapInfoHeader, 0); int nPlanes = bytesToShort(bitmapInfoHeader, 12); int nBitCount = bytesToShort(bitmapInfoHeader, 14); int nSizeImage = bytesToInt(bitmapInfoHeader, 20); int nCompression = bytesToInt(bitmapInfoHeader, 16); int nColoursUsed = bytesToInt(bitmapInfoHeader, 32); int nXPixelsMeter = bytesToInt(bitmapInfoHeader, 24); int nYPixelsMeter = bytesToInt(bitmapInfoHeader, 28); int nImportantColours = bytesToInt(bitmapInfoHeader, 36); if (nBitCount == 24) { image = read24BitBitmap(nSizeImage, nHeight, nWidth, input); } else if (nBitCount == 8) { image = read8BitBitmap(nColoursUsed, nBitCount, nSizeImage, nWidth, nHeight, input); } else { System.out.println("Not a 24-bit or 8-bit Windows Bitmap, aborting..."); image = null; } } finally { try { if (input != null) input.close(); } catch (IOException e) { } } return image; }/** * Static method to read a 8 bit bitmap * * @param nColoursUsed Number of colors used * @param nBitCount The bit count * @param nSizeImage The size of the image in bytes * @param nWidth The width of the image * @param input The input stream corresponding to the image * @throws IOException * @return A BufferedImage of the bitmap */ private static BufferedImage read8BitBitmap(int nColoursUsed, int nBitCount, int nSizeImage, int nWidth, int nHeight, InputStream input) throws IOException { int nNumColors = (nColoursUsed > 0) ? nColoursUsed : (1 & 0xff) < < nBitCount; if (nSizeImage == 0) { nSizeImage = ((((nWidth * nBitCount) + 31) & ~31) > > 3); nSizeImage *= nHeight; }int npalette[] = new int[nNumColors]; byte bpalette[] = new byte[nNumColors * 4]; readBuffer(input, bpalette); int nindex8 = 0; for (int n = 0; n < nNumColors; n++) { npalette[n] = (255 & 0xff) < < 24 | (bpalette[nindex8 + 2] & 0xff) < < 16 | (bpalette[nindex8 + 1] & 0xff) < < 8 | (bpalette[nindex8 + 0] & 0xff); nindex8 += 4; }int npad8 = (nSizeImage / nHeight) - nWidth; BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_ARGB); DataBufferInt dataBufferByte = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()); int[][] bankData = https://www.songbingjia.com/android/dataBufferByte.getBankData(); byte bdata[] = new byte[(nWidth + npad8) * nHeight]; readBuffer(input, bdata); nindex8 = 0; for (int j8 = nHeight - 1; j8 > = 0; j8--) { for (int i8 = 0; i8 < nWidth; i8++) { bankData[0][j8 * nWidth + i8] = npalette[((int) bdata[nindex8] & 0xff)]; nindex8++; } nindex8 += npad8; }return bufferedImage; }/** * Static method to read a 24 bit bitmap * * @param nSizeImage size of the imagein bytes * @param nHeight The height of the image * @param nWidth The width of the image * @param input The input stream corresponding to the image * @throws IOException * @return A BufferedImage of the bitmap */ private static BufferedImage read24BitBitmap(int nSizeImage, int nHeight, int nWidth, InputStream input) throws IOException { int npad = (nSizeImage / nHeight) - nWidth * 3; if (npad == 4 || npad < 0) npad = 0; int nindex = 0; BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_4BYTE_ABGR); DataBufferByte dataBufferByte = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()); byte[][] bankData = dataBufferByte.getBankData(); byte brgb[] = new byte[(nWidth + npad) * 3 * nHeight]; readBuffer(input, brgb); for (int j = nHeight - 1; j > = 0; j--) { for (int i = 0; i < nWidth; i++) { int base = (j * nWidth + i) * 4; bankData[0][base] = (byte) 255; bankData[0][base + 1] = brgb[nindex]; bankData[0][base + 2] = brgb[nindex + 1]; bankData[0][base + 3] = brgb[nindex + 2]; nindex += 3; } nindex += npad; }return bufferedImage; }/** * Converts bytes to an int * * @param bytes An array of bytes * @param index * @returns A int representation of the bytes */ private static int bytesToInt(byte[] bytes, int index) { return (bytes[index + 3] & 0xff) < < 24 | (bytes[index + 2] & 0xff) < < 16 | (bytes[index + 1] & 0xff) < < 8 | bytes[index + 0] & 0xff; }/** * Converts bytes to a short * * @param bytes An array of bytes * @param index * @returns A short representation of the bytes */ private static short bytesToShort(byte[] bytes, int index) { return (short) (((bytes[index + 1] & 0xff) < < 8) | (bytes[index + 0] & 0xff)); }/** * Reads the buffer * * @param in An InputStream * @param buffer An array of bytes * @throws IOException */ private static void readBuffer(InputStream in, byte[] buffer) throws IOException { int bytesRead = 0; int bytesToRead = buffer.length; while (bytesToRead > 0) { int read = in.read(buffer, bytesRead, bytesToRead); bytesRead += read; bytesToRead -= read; } } }

原始的

    推荐阅读