I am wondering if it is possible to rotate 90 degrees a video played with VLCJ. Part of the code used for displaying the video is the following:
mediaPlayerFactory = new MediaPlayerFactory();
mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
frame.setResizable(false);
frame.setUndecorated(true);
Canvas c = new Canvas();
c.setBackground(Color.black);
final JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
frame.add(p, BorderLayout.CENTER);
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
frame.setLocation(650, 200);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayer.playMedia(file);
mediaPlayer.mute(false);
The code works nicely, the video can be watched without problems, but I would like to perform a rotation on it. I have looked up on the Internet but most of the posts are about rotation of images... so anyone can help me with that? Thank you!
When you create the MediaPlayerFactory, make sure to specify the video filter and options you want as factory arguments, for example:
String[] args = {
"--video-filter",
"rotate",
"rotate-angle",
"10"
};
mediaPlayerFactory = new MediaPlayerFactory(args);
I don't think there's any way to set this dynamically while the video is playing.
The available filters come from:
$vlc --list
The available options come from:
$vlc -H
Alternatively, you could use a DirectMediaPlayer where you render the video yourself into a Graphics2D or OpenGL or whatever context and apply whatever rotation/transformation you want.
Here is mistake:
String[] args = {
"--video-filter",
"rotate",
"rotate-angle",
"10"
};
Should be
String[] args = {
"--video-filter",
"rotate",
"--rotate-angle",
"10"
};
A half a day spent
Related
I'm trying to flip through multiple images using previous and next button.
Currently, the layout is:
and this is my code:
public void createWalkthrough() {
if(currentframe != null) {
currentframe.setVisible(true);
return;
}
currentframe = new JFrame("Getting Started");
JPanel imagePanel = new JPanel();
imagePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
imageArea = new JLabel();
imagePanel.add(imageArea);
JButton previousButton = new JButton("Previous");
previousButton.addActionListener(this);
previousButton.setActionCommand("Previous");
JButton nextButton = new JButton("Next");
nextButton.addActionListener(this);
nextButton.setActionCommand("Next");
JPanel panelButtons = new JPanel();
panelButtons.setLayout(new BoxLayout(panelButtons, BoxLayout.LINE_AXIS));
panelButtons.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
panelButtons.add(Box.createRigidArea(new Dimension(10, 0)));
panelButtons.add(Box.createHorizontalGlue());
panelButtons.add(previousButton);
panelButtons.add(nextButton);
currentframe.setSize(439, 548);
currentframe.setResizable(false);
Container pane = currentframe.getContentPane();
pane.add(imagePanel, BorderLayout.CENTER);
currentframe.add(panelButtons, BorderLayout.PAGE_END);
currentframe.setVisible(true);
currentframe.requestFocusInWindow();
}
public void displayImage(int index) {
File[] imagesList = getImages();
File imageName = imagesList[index];
ImageIcon icon = new ImageIcon(imageName.getAbsolutePath());
Image image = icon.getImage().getScaledInstance(imageArea.getWidth(), imageArea.getHeight(), Image.SCALE_SMOOTH);
imageArea.setIcon(new ImageIcon(image));
}
public File[] getImages() {
File folder = new File("/Users/jwh/Desktop/img");
File[] listofImages = folder.listFiles();
return listofImages;
}
#Override
public void actionPerformed(ActionEvent e) {
if(base == null) {
return;
}
String selected = e.getActionCommand();
if(selected.equals("Previous")) {
pos = pos - 1;
if(pos < 0) {
pos = 0;
}
displayImage(pos);
} else if(selected.equals("Next")) {
pos = pos + 1;
if(pos >= getImages().length) {
pos = getImages().length;
}
displayImage(pos);
}
}
I'm not sure what I am doing wrong. I feel like I keep going around in circles by trying different things on different StackOverflow posts.
The error that I am currently getting is:
"Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
Width (0) and height (0) must be non-zero"
Any help would be greatly appreciated. Thank you!!
Your primary problem comes down to the compounding nature of your layouts and misunderstanding of the default layouts...
JPanel imagePanel = new JPanel();
imagePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
imageArea = new JLabel();
imagePanel.add(imageArea);
imagePanel, by default, is using a FlowLayout, which will attempt to honour the preferredSize of the components added to it.
JLabel, by default, has a preferredSize of 0x0.
So, when you do something like...
Image image = icon.getImage().getScaledInstance(imageArea.getWidth(), imageArea.getHeight(), Image.SCALE_SMOOTH);
you're actually passing 0x0 to the getScaledInstance method.
While there are number of ways to fix it, the simplest would be to use a layout manager which wants to fill the available space, like BorderLayout...
JPanel imagePanel = new JPanel(new BorderLayout());
imagePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
imageArea = new JLabel();
imagePanel.add(imageArea);
Feedback...
Okay, a number of small issues...
getScaledInstance does not produce the best result, nor is the fastest way to scale an image. Now, if you'd prefer not to use a library, like imgscalr, you could use something like Java: maintaining aspect ratio of JPanel background image and Quality of Image after resize very low -- Java to scale the images
It's some what of a personal thing, but I don't like the ImageIcon API, it doesn't provide feedback about why an image might not have been loaded and doesn't support a large range of image formats.
Personally, I prefer to use the ImageIO API instead. See Reading/Loading an Image
so I've searched up on this How do I put html in a JLabel in java?, and so far I've come up with this:
String labelText ="<html><FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> Text</html>";
JLabel coloredLabel =new JLabel(labelText);
note that i did not make this code, this was an answer off that question.
So far that's good, but how would i take it off the website and into that?
I've go this:
JLabel res;
res = new JLabel("<html>" + the code i got from the internet using JauntApi + "</html>");
panel.add(res);
so far, nothing is showing, which makes me think (and is kind of obvious) that i did something wrong. But yet, when i do this:
System.out.println(code from internet);
It works perfectly fine and prints out the html.
So, my problem is: how do i take the html and put it on that JPanel? Thanks, and sorry for not being very clear; I suck at explaining.
Note that i want it to look like the actual website, not just a bunch of html tags.
My whole code?
public static void main(String[] args){
final JFrame frame = new JFrame("MobaGuideLOL");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 200, 600, 600);
JPanel panel = new JPanel();
frame.add(panel);
JLabel res;
try{
UserAgent userAgent = new UserAgent();
userAgent.visit("http://www.mobafire.com/league-of-legends/ahri-guide");
System.out.println(userAgent.doc.innerHTML());
res = new JLabel("<html>" + userAgent.doc.innerHTML() + "</html>");
panel.add(res);
}catch(JauntException e){
System.err.println(e);
}
}
it's a guide for league of legends just saying
I need to build BlackJack game as an study project.
I want build it with SWING GUI. What I need it just divide the screen in 2 parts, and then to be able insert elements (in my case it's extended JButton with signed ImageIcon) using absolute (x, y) position relative to specified part.
Something like that:
I came from developing under Android, where you can work with elements in very simple way, and I feel lost in SWING. There aren't AbsoluteLayout or something like that?
Here is one example of my several attempts to this:
public void run() {
// TODO Auto-generated method stub
JFrame jFrame = new JFrame("Blackjack");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = jFrame.getContentPane();
Insets insets = pane.getInsets();
URL url = ClassLoader.getSystemClassLoader().getResource("10_of_clubs.png");
BufferedImage bi = null;
try {
bi = ImageIO.read(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Image resizedImage = bi.getScaledInstance(128, 186, 0);
ImageIcon icon = new ImageIcon(resizedImage);
ImageButton imgButton = new ImageButton(icon);
imgButton.setPreferredSize(new Dimension(128, 186));
ImageButton imgButton2 = new ImageButton(icon);
imgButton.setPreferredSize(new Dimension(128, 186));
pane.setLayout(new GridBagLayout());
JPanel headPanel = new JPanel();
JPanel headPanel2 = new JPanel();
GridBagConstraints cns = new GridBagConstraints();
cns.gridx = 0;
cns.gridy = 0;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
headPanel.setBackground(Color.RED);
headPanel.add(imgButton, cns);
GridBagConstraints cns2 = new GridBagConstraints();
cns2.gridx = 0;
cns2.gridy = 0;
cns2.weightx = 0.5;
cns2.weighty = 0.2;
cns2.anchor = GridBagConstraints.FIRST_LINE_START;
cns2.fill = GridBagConstraints.CENTER;
headPanel2.setBackground(Color.BLUE);
headPanel2.add(imgButton2, cns2);
pane.add(headPanel);
pane.add(headPanel2);
jFrame.setSize(800, 600);
jFrame.setVisible(true);
jFrame.setLocationRelativeTo(null);
}
That what I get:
Tnx.
if you want absolute layout, please take a look at: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
in general to read about layouts in java you can take a look at:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
here is all java swing components: visual guide:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
I think you can use JSplitPane (http://algo.math.ntua.gr/~symvonis/other-material/java_material/JavaTutorial/uiswing/components/splitpane.html) to create vertical separation
Since you have overlapping elements you can:
Use your existing JButtons with images inside a JLayeredPane. Put your cards on different layers for a clean rendering. Set the position of your Cards absolute with 'setBounds()'
Draw your cards with absolute position yourself using a Canvas. If you take this approach, you will also have to do your Click handling yourself (check if a click is inside a card.)
I have a problem with my tabs:
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JPanel contact = new JPanel();
contact.add(backgroundContact);
tab.add("Contacto", contact);
//tab.addTab("Contacto",new ImageIcon("images/image2.gif"), contact,"");
JPanel schedule = new JPanel();
schedule.add(backgroundSchedule);
tab.add("Horario", schedule);
//tab.addTab("Horario", new ImageIcon("images/image2.gif"), schedule,"");
JPanel cost = new JPanel();
cost.add(backgroundCost);
tab.add("Tarifas", cost);
//tab.addTab("Tarifas", new ImageIcon("images/image3.gif"), cost,"");
// Los iconos
tab.setIconAt(0, new ImageIcon("images/image1.gif"));
tab.setIconAt(1, new ImageIcon("images/image2.gif"));
tab.setIconAt(2, new ImageIcon("images/image3.gif"));
I've tried both options, but the icons are not shown. Why is it happening?
I also tried: new ImageIcon("images/im.gif") which doesn't exist and I haven any error
Try this instead:
URL urlToImage3 = this.getClass().getResource("/" + "images/image3.gif");
... new ImageIcon(urlToImage3);
You might concatenate "/" + "images/image3.gif" - I just wanted to highlight the leading /, since it is more robust to search from the root of the class-path.
If these images are an 'embedded resource' as I suspect, they will not be available by File at run-time, but should be on the class-path in one of the Jars of the app., and therefore available by URL.
I am using the gstreamer library for a Java project that needs to be able to capture an image from a webcam.
I already have the code that displays the webcam stream, I just can't figure out how to capture an image at the press of a button next to it.
I have searched the internet, but could only find pieces of code that show either the stream, or capture the image, but none illustrated both... I've tried to merge those pieces of code, but that didn't work for me either.
What do I have to do to get this to work?
public class WebcamPanel extends JPanel {
private static Pipeline pipe;
public WebcamPanel(){
String[] args = {};
args = Gst.init("Webcam", args);
pipe = new Pipeline("pipeline");
final Element videosrc = ElementFactory.make("dshowvideosrc", "source");
final Element videofilter = ElementFactory.make("capsfilter", "flt");
videofilter.setCaps(Caps.fromString("video/x-raw-yuv, width=320, height=240"));
setLayout(new GridBagLayout());
final GridBagConstraints c = new GridBagConstraints();
JButton takePic = new JButton();
takePic.setPreferredSize(new Dimension(50,50));
c.gridx = 0;
c.insets = new Insets(0,10,0,0);
add(takePic,c);
c.gridx = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
c.insets = new Insets(0,40,0,0);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
VideoComponent videoComponent = new VideoComponent();
Element videosink = videoComponent.getElement();
// This gives 2nd window with stream from webcam
// Element videosink = ElementFactory.make("xvimagesink", "sink");
pipe.addMany(videosrc, videofilter, videosink);
Element.linkMany(videosrc, videofilter, videosink);
videoComponent.setPreferredSize(new Dimension(320, 240));
add(videoComponent,c);
videoComponent.setVisible(true);
// Start the pipeline processing
pipe.setState(State.PLAYING);
}
});
}
}
Have you take a look at camerabin2? This will implement the whole camera workflow for you (viewfinder, image capture, video captue, effects, ...).
The basic approach is to either tee off a 2nd stream and capture selected images from it (e.g. use a valve ! jpegenc ! multifilesink and open the valve for selected images) or to use a output-selector to the image-saving pipe or to use a buffer-probe (hacky).