Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按值而不是参照复制[JavaCV-Frame]_Java - Fatal编程技术网

按值而不是参照复制[JavaCV-Frame]

按值而不是参照复制[JavaCV-Frame],java,Java,当我们从FrameGrabber检索一个帧时,我们得到一个对该帧的引用。 框架将添加到列表中。但是,由于我们仅引用框架,因此列表中的所有对象都指向相同的引用 因为我既不能访问对象帧,也不能访问FrameGrabber,所以我无法创建它们或。因此,没有复制 我相信,因为我们每轮都创建一个新的对象框架,所以我们得到了一个新的参考,对吗 Frame frame = new Frame(); 但是,对象帧本身是对以下内容的引用: frame = mFrameGrabber.grabFrame();

当我们从FrameGrabber检索一个帧时,我们得到一个对该帧的引用。 框架将添加到列表中。但是,由于我们仅引用框架,因此列表中的所有对象都指向相同的引用

因为我既不能访问对象帧,也不能访问FrameGrabber,所以我无法创建它们或。因此,没有复制

我相信,因为我们每轮都创建一个新的对象框架,所以我们得到了一个新的参考,对吗

Frame frame = new Frame();
但是,对象帧本身是对以下内容的引用:

frame = mFrameGrabber.grabFrame();
因此,罪魁祸首是mFrameGrabber,它每次都返回相同的引用。。帮我解决这个问题

简短:捕获帧(对象)并将内容(非引用)存储在object类型的arraylist中

也许这会有帮助

frame = (Frame)mFrameGrabber.grabFrame().clone();
也许这会有帮助

frame = (Frame)mFrameGrabber.grabFrame().clone();

问题现在已经解决了。查看类/对象“框架”,得出的结论是,内部唯一需要深度复制的组件是:

public Buffer[] image;
在课堂中深入一点,可以看到以下内容:

ByteBuffer buffer = ByteBuffer.allocateDirect(imageHeight * imageStride * pixelSize).order(ByteOrder.nativeOrder());
image[0] = buffer;  
所以缓冲区的索引0包含一个ByteBuffer,它是不可序列化的!于是我开始在网上深入研究如何深度复制这样一个物体。结果很好

下面是解决方案。谢谢大家的反馈

private Frame copyFrame(Frame frame)
{
    // Frame that will hold the copy
    Frame cFrame = new Frame(640, 480, 8, 3);

    // Copy the byte buffer from frame
    ByteBuffer originalByteBuffer = (ByteBuffer) frame.image[0];

    // Create the clone buffer with same capacity as the original
    ByteBuffer cloneBuffer = ByteBuffer.allocateDirect(originalByteBuffer.capacity());
    //ByteBuffer cloneBuffer = deepCopy(originalByteBuffer);

    // Save parameters from the original byte buffer
    int position = originalByteBuffer.position();
    int limit = originalByteBuffer.limit();


    // Set range to the entire buffer
    originalByteBuffer.position(0).limit(originalByteBuffer.capacity());

    // Read from original and put into clone
    cloneBuffer.put(originalByteBuffer);

    // Set the order same as original
    cloneBuffer.order(originalByteBuffer.order());

    // Set clone position to 0 and set the range as the original
    cloneBuffer.position(0);
    cloneBuffer.position(position).limit(limit);

    // Save the clone 
    cFrame.image[0] = cloneBuffer;      

    return cFrame;
}

问题现在已经解决了。查看类/对象“框架”,得出的结论是,内部唯一需要深度复制的组件是:

public Buffer[] image;
在课堂中深入一点,可以看到以下内容:

ByteBuffer buffer = ByteBuffer.allocateDirect(imageHeight * imageStride * pixelSize).order(ByteOrder.nativeOrder());
image[0] = buffer;  
所以缓冲区的索引0包含一个ByteBuffer,它是不可序列化的!于是我开始在网上深入研究如何深度复制这样一个物体。结果很好

下面是解决方案。谢谢大家的反馈

private Frame copyFrame(Frame frame)
{
    // Frame that will hold the copy
    Frame cFrame = new Frame(640, 480, 8, 3);

    // Copy the byte buffer from frame
    ByteBuffer originalByteBuffer = (ByteBuffer) frame.image[0];

    // Create the clone buffer with same capacity as the original
    ByteBuffer cloneBuffer = ByteBuffer.allocateDirect(originalByteBuffer.capacity());
    //ByteBuffer cloneBuffer = deepCopy(originalByteBuffer);

    // Save parameters from the original byte buffer
    int position = originalByteBuffer.position();
    int limit = originalByteBuffer.limit();


    // Set range to the entire buffer
    originalByteBuffer.position(0).limit(originalByteBuffer.capacity());

    // Read from original and put into clone
    cloneBuffer.put(originalByteBuffer);

    // Set the order same as original
    cloneBuffer.order(originalByteBuffer.order());

    // Set clone position to 0 and set the range as the original
    cloneBuffer.position(0);
    cloneBuffer.position(position).limit(limit);

    // Save the clone 
    cFrame.image[0] = cloneBuffer;      

    return cFrame;
}

请检查您是否意识到创建了新的帧对象,即使您不使用它(时间尚未过去)?另外,我认为创建实例是多余的,因为您应该从
grabFrame()
.frame=mFrameGrabber.grabFrame().clone()获取一个实例;可能是吗?这并不能解决您的问题,但您应该摆脱
新帧()
。进行更深入的复制,这是我认为的唯一方法…请检查您是否意识到您创建了新帧对象,即使您不使用它(时间尚未过去)?另外,我认为创建实例是多余的,因为您应该从
grabFrame()
.frame=mFrameGrabber.grabFrame().clone()获取一个实例;可能是吗?这并不能解决您的问题,但您应该摆脱
新框架()
。进行更深入的复制,我认为这是您唯一的方法…他很好地讨论了什么是更好的克隆或复制构造函数,这行代码不会编译;)。因为您需要实现可克隆接口的克隆功能,或者为您的框架类实现复制构造函数。并且因为
clone
不返回
Frame
。永远不会。当然,你需要从对象投射到帧。好的观点。他很好地讨论了什么是更好的克隆或复制构造函数,这行代码不会编译;)。因为您需要实现可克隆接口的克隆功能,或者为您的框架类实现复制构造函数。并且因为
clone
不返回
Frame
。永远不会。当然,你需要从对象投射到帧。说得好。