2012-02-23 232 views
2

我想使用攝像頭作爲android模擬器的攝像頭使用攝像頭作爲android模擬器攝像頭

我提到this但得到了下面的錯誤,同時運行WebcamBroadcaster無法找到合適的球員

由於在這個環節提到的,我已經創建WebcamBroadcaster作爲獨立的Java應用程序,我有工作的攝像頭連接到我的機器。

當我直接運行模擬器的照相機(不在任何應用程序中)時,它可以工作。但我試圖在我的應用程序中使用相機,使用WebcamBroadcaster,但它不起作用。相反,它是給棋盤式的動畫。

我是新來的android開發,所以任何幫助讚賞!

WebcamBroadcaster.java

public class WebcamBroadcaster { 

public static boolean RAW = false; 

private static Player createPlayer(int width, int height) { 
    try { 
     Vector<CaptureDeviceInfo> devices = CaptureDeviceManager 
       .getDeviceList(null); 
     for (CaptureDeviceInfo info : devices) { 
      DataSource source; 
      Format[] formats = info.getFormats(); 
      for (Format format : formats) { 
       if ((format instanceof RGBFormat)) { 
        RGBFormat rgb = (RGBFormat) format; 
        Dimension size = rgb.getSize(); 
        if (size.width != width || size.height != height) 
         continue; 
        if (rgb.getPixelStride() != 3) 
         continue; 
        if (rgb.getBitsPerPixel() != 24) 
         continue; 
        if (rgb.getLineStride() != width * 3) 
         continue; 
        MediaLocator locator = info.getLocator(); 
        source = Manager.createDataSource(locator); 
        source.connect(); 
        System.out.println("RGB Format Found"); 
        ((CaptureDevice) source).getFormatControls()[0] 
          .setFormat(rgb); 
       } else if ((format instanceof YUVFormat)) { 
        YUVFormat yuv = (YUVFormat) format; 
        Dimension size = yuv.getSize(); 
        if (size.width != width || size.height != height) 
         continue; 
        MediaLocator locator = info.getLocator(); 
        source = Manager.createDataSource(locator); 
        source.connect(); 
        System.out.println("YUV Format Found"); 
        ((CaptureDevice) source).getFormatControls()[0] 
          .setFormat(yuv); 
       } else { 
        continue; 
       } 

       return Manager.createRealizedPlayer(source); 
      } 
     } 
    } catch (IOException e) { 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } catch (NoPlayerException e) { 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } catch (CannotRealizeException e) { 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } catch (NoDataSourceException e) { 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } 
    return null; 
} 

public static void main(String[] args) { 
    int[] values = new int[args.length]; 
    for (int i = 0; i < values.length; i++) { 
     values[i] = Integer.parseInt(args[i]); 
    } 
    //length of args and values is **0** 
    WebcamBroadcaster wb; 
    if (values.length == 0) { 
     wb = new WebcamBroadcaster(); 
    } else if (values.length == 1) { 
     wb = new WebcamBroadcaster(values[0]); 
    } else if (values.length == 2) { 
     wb = new WebcamBroadcaster(values[0], values[1]); 
    } else { 
     wb = new WebcamBroadcaster(values[0], values[1], values[2]); 
    } 

    wb.start(); 
} 

public static final int DEFAULT_PORT = 9889; 
public static final int DEFAULT_WIDTH = 320; 
public static final int DEFAULT_HEIGHT = 240; 

private final Object lock = new Object(); 

private final int width; 
private final int height; 
private final int port; 

private boolean running; 

private Player player; 
private FrameGrabbingControl control; 
private boolean stopping; 
private Worker worker; 

public WebcamBroadcaster(int width, int height, int port) { 
    this.width = width; 
    this.height = height; 
    this.port = port; 
} 

public WebcamBroadcaster(int width, int height) { 
    this(width, height, DEFAULT_PORT); 
} 

public WebcamBroadcaster(int port) { 
    this(DEFAULT_WIDTH, DEFAULT_HEIGHT, port); 
} 

public WebcamBroadcaster() { 
    this(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_PORT); 
} 

public void start() { 
    synchronized (lock) { 
     if (running) 
      return; 
     player = createPlayer(width, height); 
     if (player == null) { 
    //**THIS IS THE MAIN PROBLEM** 
      System.err.println("Unable to find a suitable player"); 
      return; 
     } 
     System.out.println("Starting the player"); 
     player.start(); 
     control = (FrameGrabbingControl) player 
       .getControl("javax.media.control.FrameGrabbingControl"); 
     worker = new Worker(); 
     worker.start(); 
     System.out.println("Grabbing frames"); 
     running = true; 
    } 
} 

public void stop() throws InterruptedException { 
    synchronized (lock) { 
     if (!running) 
      return; 
     if (player != null) { 
      control = null; 
      player.stop(); 
      player = null; 
     } 
     stopping = true; 
     running = false; 
     worker = null; 
    } 
    try { 
     worker.join(); 
    } finally { 
     stopping = false; 
    } 
} 

private class Worker extends Thread { 

    private final int[] data = new int[width * height]; 

    @Override 
    public void run() { 
     ServerSocket ss; 
     try { 
      ss = new ServerSocket(port); 

     } catch (IOException e) { 
      e.printStackTrace(); 
      return; 
     } 

     while (true) { 
      FrameGrabbingControl c; 
      synchronized (lock) { 
       if (stopping) 
        break; 
       c = control; 
      } 
      Socket socket = null; 
      try { 
       socket = ss.accept(); 

       Buffer buffer = c.grabFrame(); 
       BufferToImage btoi = new BufferToImage(
         (VideoFormat) buffer.getFormat()); 
       BufferedImage image = (BufferedImage) btoi 
         .createImage(buffer); 

       if (image != null) { 
        OutputStream out = socket.getOutputStream(); 
        if (RAW) { 
         image.getWritableTile(0, 0).getDataElements(0, 0, 
           width, height, data); 
         image.releaseWritableTile(0, 0); 
         DataOutputStream dout = new DataOutputStream(
           new BufferedOutputStream(out)); 
         for (int i = 0; i < data.length; i++) { 
          dout.writeInt(data[i]); 
         } 
         dout.close(); 
        } else { 
         ImageIO.write(image, "JPEG", out); 
        } 
       } 

       socket.close(); 
       socket = null; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       if (socket != null) 
        try { 
         socket.close(); 
        } catch (IOException e) { 
         /* ignore */ 
        } 
      } 

     } 

     try { 
      ss.close(); 
     } catch (IOException e) { 
      /* ignore */ 
     } 
    } 

} 

} 
+0

請仔細看看這個http://stackoverflow.com/questions/2884620/using-camera-in-the-android-emulator – Navdroid 2012-02-23 13:19:03

+0

我已經看過它,並遵循相同的教程。但是我收到上面提到的錯誤。 – GAMA 2012-02-24 04:24:15

回答

1

使用 「JMyron」 而不是 「JMF」 作爲JMF已被棄用。

相關問題