javacpp ffmpeg:IplImage在java.nio.DirectByteBuffer.put发送java.nio.BufferOverflowException

时间:2018-07-04 23:31:57

标签: java opencv javacv iplimage javacpp

我想要的

我想获取16位灰度图像,将其作为字节数组存储,然后将这些数组发送到ffmpeg / opencv以创建包含所有图像的mp4视频。不会有声音。

我要做什么

作为一个例子,我正在创建8bit灰色的伪图像,然后使用它们填充视频。我正在为Java使用bytedeco库。库设置正确。全部如下面的代码所示:

import java.awt.EventQueue;
import java.nio.Buffer;
import java.nio.ByteBuffer;

import static org.bytedeco.javacpp.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_calib3d.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;*/
import javax.swing.JFrame;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;
import static org.bytedeco.javacpp.opencv_core.*;

public class testt{

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    testt window = new testt();
                    window.frame.setVisible(true);

                    OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
                    FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("C:\\sample.mp4",200,100); 
                    try {
                        recorder.setFrameRate(1);
                        recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
                        recorder.setVideoBitrate(1000000);
                        recorder.setFormat("mp4");
                        recorder.setVideoQuality(0);
                        recorder.start();

                        byte[] bytes = new byte[20000];                     
                        //all black
                        for (int j = 0; j<bytes.length; j++){
                            bytes[j]=-127;
                        }

                        for (int i=0; i<1000; i++){

                            if (teta.length*(1+i)<20000) {
                                for (int j = teta.length*i; j<teta.length*(i+1); j++){
                                    teta[j]=50;
                                }
                            }

                            IplImage image = IplImage.create(cvSize(200, 100),IPL_DEPTH_8U,1);
                            image.asByteBuffer().put(bytes);
                            recorder.record(grabberConverter.convert(image));
                        }
                        recorder.stop();
                    }catch (org.bytedeco.javacv.FrameRecorder.Exception e){
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public testt() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

问题

从上面的代码中我收到:

  

java.nio.BufferOverflowException在   java.nio.DirectByteBuffer.put(未知源)   java.nio.ByteBuffer.put(未知源)   在以下位置运行test.testt $ 1.run(testt.java:47)   java.awt.event.InvocationEvent.dispatch(来源未知)   java.awt.EventQueue.dispatchEventImpl(未知源)在   java.awt.EventQueue.access $ 500(未知源)   java.awt.EventQueue $ 3.run(未知源)   java.awt.EventQueue $ 3.run(未知源)   java.security.AccessController.doPrivileged(本机方法),位于   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(未知   源)位于java.awt.EventQueue.dispatchEvent(未知源)位于   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     在java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   源)位于java.awt.EventDispatchThread.pumpEvents(未知源)     在java.awt.EventDispatchThread.pumpEvents(未知来源)   java.awt.EventDispatchThread.run(未知来源)

我做错了什么?

0 个答案:

没有答案