很难解释,所以我拿出我的手机并录下了我遇到的问题:
http://www.youtube.com/watch?v=KdOeNdE8W2Q
简单地说,图像(您可以看到底部的边框,实现为 JLabel)应该始终保留在那里,而是出现 1 秒钟,然后消失,然后偶尔闪烁。
import java.awt.Button;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.imgscalr.Scalr;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
public class WebcamPanelExample extends JFrame {
static Webcam webcam;
static JFrame window;
public static void main(String[] args) throws IOException {
window = new JFrame("Test webcam panel");
Dimension[] nonStandardResolutions = new Dimension[] {
WebcamResolution.HD720.getSize(),
};
webcam = Webcam.getDefault();
webcam.setCustomViewSizes(nonStandardResolutions);
webcam.setViewSize(WebcamResolution.HD720.getSize());
WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSLimited(false);
panel.setFillArea(true);
window.add(panel);
////////////
BufferedImage image2 = ImageIO.read(new File("F:/DATA/images/image_area.png"));
JLabel guiBorder = new JLabel(new ImageIcon( image2 ));
guiBorder.setSize(new Dimension(1280,720));
guiBorder.setLocation(0, 0);
window.add( guiBorder );
///////////
Button button = new Button("Do Something");
button.setSize(new Dimension(200,32));
button.setLocation(540, 670);
window.add(button);
///////////
window.setComponentZOrder(button, 0);
window.setComponentZOrder(guiBorder, 1);
window.setComponentZOrder(panel, 2);
///////////
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
window.validate();
window.setSize(1280,720);
window.setResizable(false);
window.setVisible(true);
}
}
此外,如果它很重要:
- 使用https://github.com/sarxos/webcam-capture作为网络摄像头库。
- 使用 Windows 8 64 位但 Java 32 位(需要一些东西才能工作)
关于问题的潜在原因的任何想法?
编辑(安德鲁·汤普森)
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Date;
import java.io.File;
class AcceleratePlayback {
public static void main(String[] args) throws Exception {
int playBackSpeed = 3;
File soundFile;
if (args.length>0) {
try {
playBackSpeed = Integer.parseInt(args[0]);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
System.out.println("Playback Rate: " + playBackSpeed);
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
soundFile = chooser.getSelectedFile();
System.out.println("FILE: " + soundFile);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
AudioFormat af = ais.getFormat();
int frameSize = af.getFrameSize();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[2^16];
int read = 1;
while( read>-1 ) {
read = ais.read(b);
if (read>0) {
baos.write(b, 0, read);
}
}
System.out.println("End entire: \t" + new Date());
//This is the important bit
byte[] b1 = baos.toByteArray();
byte[] b2 = new byte[b1.length/playBackSpeed];
for (int ii=0; ii<b2.length/frameSize; ii++) {
for (int jj=0; jj<frameSize; jj++) {
int b3=0;
for (int kk = 0; kk < playBackSpeed; kk++){
b3 = b3+(int)b1[(ii*frameSize*playBackSpeed)+jj+kk];
}
b3 = b3/playBackSpeed;
b2[(ii*frameSize)+jj] = (byte)b3;
}
}
//ends here
System.out.println("End sub-sample: \t" + new Date());
ByteArrayInputStream bais = new ByteArrayInputStream(b2);
AudioInputStream aisAccelerated = new AudioInputStream(bais, af, b2.length);
Clip clip = AudioSystem.getClip();
clip.open(aisAccelerated);
clip.loop(2*playBackSpeed);
clip.start();
JOptionPane.showMessageDialog(null, "Exit?");
}
}