I want to capture an image using an available webcam. I have successfully accessed the webcam but I couldn't stop the webcam process. I want to stop the webcam process using a stop button. How can I accomplish this? This my code:
public Component componen() throws IOException , NoPlayerException, CannotRealizeException
{
Component comp_video;
MediaLocator loo = new MediaLocator("vfw://0");
try {
broadcast = Manager.createRealizedPlayer(loo);
} catch (IOException ex) {
Logger.getLogger(CapturImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoPlayerException ex) {
Logger.getLogger(CapturImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(CapturImage.class.getName()).log(Level.SEVERE, null, ex);
}
broadcast.start();
if((comp_video = broadcast.getVisualComponent()) != null)
{
comp_video.setSize(321,228);
return comp_video;
}
else
{
return null;
}
}
public void capture_image()
{
FrameGrabbingControl grab = (FrameGrabbingControl) broadcast.getControl("javax.media.control.FrameGrabbingControl");
javax.media.Buffer buff = grab.grabFrame();
BufferToImage buffer =new BufferToImage((VideoFormat) buff.getFormat());
img = buffer.createImage(buff);
}
public void set_iamge_label(final JLabel lb)
{
Thread web = new Thread(){
public void run(){
capture_image();
Rectangle rect = lb.getBounds();
Image img1 = img.getScaledInstance(rect.width,rect.height,Image.SCALE_DEFAULT);
lb.setIcon(new javax.swing.ImageIcon(img1));
}
};
web.start();
}
Related
I read a file and set the text(about 4000KB of size) into a JTextArea. It freezes my application. Following is my code snippet. I appreciate your suggestions....
public void setText(final JTextArea textArea)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
BufferedReader from = null;
try
{
from = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
System.out.println("Started..........");
textArea.read(from, file.getName());
System.out.println("Completed..........");
}
catch (Exception ex)
{
Logger.getLogger(FileTools.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try
{
from.close();
}
catch (IOException ex)
{
Logger.getLogger(FileTools.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
Aim:
Play video in blackberry device from remote server.
Current Output
Blank screen and this message: "Press space to start/stop/resume playback."
My code
public class MyApp extends UiApplication
{
private Player player;
private VideoControl videoControl;
public static void main(String[] args)
{
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp()
{
MainScreen ms = new MainScreen();
public boolean onClose()
{
player.close();
videoControl.setVisible(false);
close();
return true;
}
protected boolean keyChar(char c, int status, int time)
{
boolean retVal = false;
if (c == Characters.SPACE)
{
if (player.getState() == Player.STARTED)
{
//Stop playback.
try
{
player.stop();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.toString());
}
}
else
{
//Start playback.
try
{
player.start();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.toString());
}
}
retVal = true;
}
return retVal;
}
};
ms.setTitle(new LabelField("Let's play some video..."));
LabelField lf = new LabelField("Press space to start/stop/resume playback.");
ms.add(lf);
pushScreen(ms);
try
{
player = Manager.createPlayer("http://224.1.2.3:12344:8082/ACSATraffic/blackberry.3gp");
player.realize();
//Create a new VideoControl.
videoControl = (VideoControl)player.getControl("VideoControl");
//Initialize the video mode using a Field.
videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
videoControl.setVisible(true);
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
}
}
What am I doing wrong here?
Or else is there any sample for playing video from local and
server? A link would be greatly appreciated.
My LWUIT video component floats over my commands. Any idea how I can fix this? My code is below.
public void showQuickProfileForm() {
Form f = new Form();
VideoComponent videoComponent = null;
try {
videoComponent = VideoComponent.createVideoPeer("capture://image");
} catch (IOException e) {
// e.printStackTrace();
try {
videoComponent = VideoComponent.createVideoPeer("capture://video");
} catch (IOException ex) {
// ex.printStackTrace();
}
}
videoComponent.setPreferredH((int) (Display.getInstance().getDisplayHeight() * 0.8));
videoComponent.setPreferredW(Display.getInstance().getDisplayWidth());
Player player = (Player) videoComponent.getNativePeer();
try {
player.realize();
player.start();
} catch (MediaException ex) {
ex.printStackTrace();
}
VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
videoComponent.start();
f.addComponent(videoComponent);
f.addCommand(new Command("capture", 1));
f.show();
}
public void actionPerformed(ActionEvent evt) {
if (evt.getCommand().getId() == 1) {
// midlet.destroyApp(true);
Form d = new Form();
d.show();
}
}
Use this after initializing video control, change width and height as you see fit :
videoControl.setDisplaySize(width + 4, height - 25);
I want to make a little programm that makes a live-stream for Desktop.
It should be so that you send pictures to an echo-server and he response it to the clients.
There you get be draw the Images. Side by Side. And so it is like a movie(or something like that).
But I always get an indexoutofboundsexception. Where is the error or how can I improve my program.
The ImageIO.write lines thows the Error
//Client Main
public class Main {
public static void main(String[] args) {
Frame frm = new Frame();
Frame.Client client;
frm.setLayout(null);
frm.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
frm.setResizable(false);
frm.setSize(1600,900);
frm.setVisible(true);
}
}
// get and send the Desktopimage
public class desktopCapture {
Robot robo;
BufferedImage screenImage;
Rectangle bounding;
public desktopCapture() {
try {
bounding = new Rectangle(0,0,1600,900);
robo = new Robot();
} catch (AWTException e) {e.printStackTrace();}
}
public void sendScreenCapture(Socket client) {
screenImage = robo.createScreenCapture(bounding);
try {
ImageIO.write(screenImage, "png", client.getOutputStream());
} catch (IOException e) {e.printStackTrace();}
}
}
// in Frame two function for actionListener Objects, so I can say who streams his Desktop and which get only the Images to.
public void readImage() {
while(true) {
try {
while((screenImage = ImageIO.read(in)) != null){
repaintScreen();
}
} catch (IOException e) {e.printStackTrace();}
}
}
public void sendImage() {
try {
while(true){
dC.sendScreenCapture(client);
System.out.println("read1");
while((screenImage = ImageIO.read(in)) != null){
System.out.println("read2");
ImageIO.write(screenImage, "png", new File("image1.png"));
Thread.sleep(250);
}
repaintScreen();
screenImage = null;
}
} catch (IOException | InterruptedException e) {e.printStackTrace();}
}
}
}
//Thread for a Client
public class handler implements Runnable {
Socket client;
OutputStream out;
InputStream in;
PrintWriter writer;
BufferedImage image;
public handler(Socket client) {
this.client = client;
}
#Override
public void run() {
try {
in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while(true) {
System.out.println("write1");
while((image = ImageIO.read(in)) != null){
System.out.println("write2");
for(int i = 1;i <= Server.connectionArray.size();i++){
Socket TEMP_SOCK = (Socket)Server.connectionArray.get(i-1);
out = TEMP_SOCK.getOutputStream();
writer = new PrintWriter(out);
ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
System.out.println("write3");
}
image = null;
}
}
} catch (IOException e) {e.printStackTrace();}
}
}
I would change your for loop to:
int count = Server.connectionArray.size()
int index = count - 1;
for(int i = 0;i < count; i++){
Socket TEMP_SOCK = (Socket)Server.connectionArray.get(index);
out = TEMP_SOCK.getOutputStream();
writer = new PrintWriter(out);
ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
System.out.println("write3");
}
I want to do 2 task at same time:
Play an audio file.
Read raw data of it to do something
Here is my code:
String wavPath = "file:///" + currentPath + fileName;
FileConnection fc;
try {
fc = (FileConnection) Connector.open( wavPath );
if ( !fc.exists() ) {
throw new IOException( "File does not exists." );
}
InputStream is = fc.openInputStream();
// to do something with raw data as print samples of it
Player player = Manager.createPlayer( wavPath );
player.realize();
player.prefetch();
player.start();
} catch ( IOException e1 ) {
e1.printStackTrace();
}
But nothing run, audio file doesn't not run. If I remove line:
InputStream is = fc.openInputStream();
Audio file run very well. But I want to do 2 task at same time, i don't know how to do it. Anybody can help me ?
I have tried use 2 thread, but it still doesn't work, audio file run (thread 1) but thread 2 not run:
new Thread( new Runnable() {
public void run() {
try {
Manager.createPlayer( "file:///E:/" + fileName ).start();
} catch ( MediaException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}).start();
new Thread( new Runnable() {
public void run() {
FileConnection fc;
try {
fc = (FileConnection) Connector.open( "file:///E:/" + fileName );
InputStream is = fc.openInputStream();
byte[] b = new byte[10];
int length = is.read( b, 0, 10 );
for ( int i = 0; i < length; i++ ) {
form.append( b[i] + "" );
}
} catch ( IOException e ) {
e.printStackTrace();
}
}
}).start();
why don't you use threading...
create a thread to play the wav file
and use another thread to do read a file...
refer here for further details on threading in J2ME
public class View_PlayMidlet extends MIDlet {
PlayerThread musicPlayer = new PlayerThread();
public void startApp() {
String fileName = "file://e:/abcd.wav";
musicPlayer.setPlayableFile(fileName);
FileConnection fc = null;
InputStream is = null;
String fileContent = null;
try {
fc = (FileConnection) Connector.open("file:///E:/" + fileName);
is = fc.openInputStream();
byte[] b = new byte[10];
int length = is.read(b, 0, 10);
fileContent = new String(b);
// display the content of fileContent variable in a form.
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
}
}
if (fc != null) {
try {
fc.close();
} catch (IOException ex) {
}
}
}
// by this time the file is displayed & you can start playing the file.
Thread t = new Thread(musicPlayer);
t.start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
public class PlayerThread implements Runnable {
private String fileName;
private Player player;
public PlayerThread() {
}
public void run() {
try {
player = Manager.createPlayer(fileName);
player.prefetch();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopMusic() {
try {
player.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setPlayableFile(String fileName) {
this.fileName=fileName;
}
}
this would roughly be what you are looking for.
Your Player instances are garbage collected. Also, file access during playback will likely be 'implementation specific', meaning that it will work on some models and not others. So read the data first, copy it to another file etc, if you want something solid.
You can always go the DataSource-path too.