Advertisement
sombriks

Untitled

Apr 8th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.nio.file.Paths;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.concurrent.TimeUnit;
  11. import java.util.logging.Logger;
  12.  
  13. import javax.imageio.ImageIO;
  14.  
  15. import com.xuggle.mediatool.IMediaWriter;
  16. import com.xuggle.mediatool.ToolFactory;
  17. import com.xuggle.xuggler.ICodec;
  18.  
  19. public class MjpgUtil {
  20.  
  21.     private static final Logger LOG = Logger.getAnonymousLogger();
  22.  
  23.     public static class Frame {
  24.  
  25.         private long time;
  26.         private byte[] image;
  27.  
  28.         public Frame() {
  29.         }
  30.  
  31.         public Frame(long time, byte[] image) {
  32.             this.image = image;
  33.             this.time = time;
  34.         }
  35.  
  36.         public long getTime() {
  37.             return time;
  38.         }
  39.  
  40.         public void setTime(long time) {
  41.             this.time = time;
  42.         }
  43.  
  44.         public byte[] getImage() {
  45.             return image;
  46.         }
  47.  
  48.         public void setImage(byte[] image) {
  49.             this.image = image;
  50.         }
  51.  
  52.     }
  53.  
  54.     public static int trataHeader(InputStream in) throws IOException {
  55.  
  56.         // --myboundary\r\n
  57.         // Content-Type: image/jpeg\r\n
  58.         // Content-Length: 37459\r\n\r\n
  59.         byte buf1[] = new byte[1];
  60.         int four = 0;
  61.         List<Byte> bytes = new LinkedList<Byte>();
  62.         while (four < 4) {
  63.             in.read(buf1);
  64.             if (buf1[0] == 13 || buf1[0] == 10) {
  65.                 four++;
  66.             } else {
  67.                 four = 0;
  68.                 bytes.add(buf1[0]);
  69.             }
  70.         }
  71.         byte bts[] = new byte[bytes.size()];
  72.         int i = bts.length;
  73.         while (i-- > 0) {
  74.             bts[i] = bytes.get(i);
  75.         }
  76.         String header = new String(bts);
  77.         header = header.replaceFirst(".*Content-Length: (\\d+).*", "$1");
  78.         // System.out.println(header);
  79.         return Integer.parseInt(header);
  80.     }
  81.  
  82.     public static byte[] trataImagem(int len, InputStream in) throws Exception {
  83.         try (ByteArrayOutputStream out = new ByteArrayOutputStream(len)) {
  84.             byte buf[] = new byte[len];
  85.             int i = -1;
  86.             int tot = 0;
  87.             while (tot < len && (i = in.read(buf)) > -1) {
  88.                 tot += i;
  89.                 out.write(buf, 0, i);
  90.             }
  91.             return out.toByteArray();
  92.         }
  93.     }
  94.  
  95.     public static void getVideo(List<Frame> frames, int step, String fName)
  96.             throws Exception {
  97.         if (frames.size() > 0) {
  98.             LOG.info("creating video with "+frames.size()+" frames");
  99.             IMediaWriter writer = ToolFactory.makeWriter(fName);
  100.             writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, 640, 480);
  101.             long t = 0;
  102.             // long fps = 1000 / cfg.getFps();
  103.             for (Frame p : frames) {
  104.                 try (ByteArrayInputStream in = //
  105.                 new ByteArrayInputStream(p.getImage())) {
  106.                     BufferedImage img = ImageIO.read(in);
  107.                     writer.encodeVideo(0, img, t, TimeUnit.MILLISECONDS);
  108.                     t += step;
  109.                 }
  110.             }
  111.             writer.close();
  112.             if (Paths.get(fName).toFile().length() <= 0)
  113.                 LOG.warning("empty video!");
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement