SurfaceView on xml

All your problems with Audio, Video and Images.

SurfaceView on xml

Postby bobalot » Fri Jul 04, 2008 9:00 pm

hi, im currently trying to get a surface and other items, (buttons etc) into the view inside a tab, obviously the best way to do this is to is to use xml and to map the surface handler onto the surface view,

But every way i try to do this i always get a null pointer exception,
anyone have any ideas i will be most gratefull

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package Camera;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.Context;
  9.  
  10. import android.graphics.Canvas;
  11.  
  12. import android.hardware.CameraDevice;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.Surface;
  17.  
  18. import android.view.SurfaceHolder;
  19.  
  20. import android.view.SurfaceView;
  21.  
  22. import android.view.View;
  23.  
  24. import android.view.Window;
  25.  
  26. import android.widget.TabHost;
  27.  
  28. import android.widget.TabHost.TabSpec;
  29.  
  30.  
  31.  
  32. public class Camera extends Activity {    
  33.  
  34.   private TabHost myTabHost;
  35.  
  36.   private Preview mPreview;
  37.  
  38.  
  39.  
  40. //  private PreviewThread mPreviewThread;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.   @Override
  49.  
  50.         protected void onCreate(Bundle icicle) {
  51.  
  52.         super.onCreate(icicle);
  53.  
  54.         SurfaceHolder  mHolder;
  55.  
  56.         // Hide the window title.
  57.  
  58.        //requestWindowFeature(Window.FEATURE_NO_TITLE);
  59.  
  60.        // setContentView(R.layout.main);
  61.  
  62.  
  63.  
  64.        
  65.  
  66.         setContentView(R.layout.tabs);
  67.  
  68.  
  69.  
  70.         this.myTabHost = (TabHost)this.findViewById(R.id.tabhost);
  71.  
  72.         this.myTabHost.setup();
  73.  
  74.        
  75.  
  76.         TabSpec ts1 = myTabHost.newTabSpec("TAB1");
  77.  
  78.         ts1.setIndicator("Take Photo", getResources().getDrawable(R.drawable.cameraphoto));
  79.  
  80.         ts1.setContent(R.id.grid_set_menu_page1);
  81.  
  82.    
  83.  
  84.         this.myTabHost.addTab(ts1);
  85.  
  86.         //  mPreview = new Preview(this);
  87.  
  88.         //   ts1.setContent(mPreview);
  89.  
  90. //           mPreview.mHolder.getSurface().
  91.  
  92.        
  93.  
  94.        
  95.  
  96.         TabSpec ts2 = myTabHost.newTabSpec("TAB2");
  97.  
  98.         ts2.setIndicator("Album", getResources().getDrawable(R.drawable.driveharddisk));
  99.  
  100.         ts2.setContent(R.id.grid_set_menu_page2);
  101.  
  102.         this.myTabHost.addTab(ts2);
  103.  
  104.        
  105.  
  106.         TabSpec ts3 = myTabHost.newTabSpec("TAB3");
  107.  
  108.         ts3.setIndicator("settings", getResources().getDrawable(R.drawable.networkwireless));
  109.  
  110.         ts3.setContent(R.id.grid_set_menu_page3);
  111.  
  112.         this.myTabHost.addTab(ts3);
  113.  
  114.        
  115.  
  116.         this.myTabHost.setCurrentTab(0);
  117.  
  118.        
  119.  
  120.     //    SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.photo);
  121.  
  122.     //    mPreview = new Preview(mSurfaceView);
  123.  
  124.  
  125.  
  126.        
  127.  
  128.     }
  129.  
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. //----------------------------------------------------------------------
  139.  
  140.  
  141.  
  142. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  143.  
  144.     SurfaceHolder  mHolder;
  145.  
  146.     private PreviewThread mPreviewThread;
  147.  
  148.     private boolean mHasSurface;
  149.  
  150.     SurfaceHolder SHolder;
  151.  
  152.     private SurfaceView SView;
  153.  
  154.    
  155.  
  156.     Preview(Context context) {
  157.  
  158.         super(context);
  159.  
  160.  
  161.  
  162.         // Install a SurfaceHolder.Callback so we get notified when the
  163.  
  164.         // underlying surface is created and destroyed.
  165.  
  166.         //mHolder = getHolder();
  167.  
  168.  
  169.  
  170.        
  171.  
  172.         /////////////////////////////////////////////////////////////
  173.  
  174.         SView = (SurfaceView) findViewById(R.id.photo);
  175.  
  176. //SHolder = SView.getHolder();
  177.  
  178. mHolder = SView.getHolder();
  179.  
  180. /////////////////////////////////////////////////////
  181.  
  182.  
  183.  
  184. mHolder.addCallback(this);
  185.  
  186. mHasSurface = false;
  187.  
  188.         // In this example, we hardcode the size of the preview. In a real
  189.  
  190.         // application this should be more dynamic. This guarantees that
  191.  
  192.         // the uderlying surface will never change size.
  193.  
  194.         mHolder.setFixedSize(320, 240);
  195.  
  196.     }
  197.  
  198.  
  199.  
  200.     public void resume() {
  201.  
  202.         // We do the actual acquisition in a separate thread. Create it now.
  203.  
  204.         if (mPreviewThread == null) {
  205.  
  206.             mPreviewThread = new PreviewThread();
  207.  
  208.             // If we already have a surface, just start the thread now too.
  209.  
  210.             if (mHasSurface == true) {
  211.  
  212.                 mPreviewThread.start();
  213.  
  214.             }
  215.  
  216.         }
  217.  
  218.     }
  219.  
  220.  
  221.  
  222.     public void pause() {
  223.  
  224.         // Stop Preview.
  225.  
  226.         if (mPreviewThread != null) {
  227.  
  228.             mPreviewThread.requestExitAndWait();
  229.  
  230.             mPreviewThread = null;
  231.  
  232.         }
  233.  
  234.     }
  235.  
  236.  
  237.  
  238.     public void surfaceCreated(SurfaceHolder holder) {
  239.  
  240.         // The Surface has been created, start our main acquisition thread.
  241.  
  242.         mHasSurface = true;
  243.  
  244.         if (mPreviewThread != null) {
  245.  
  246.             mPreviewThread.start();
  247.  
  248.         }
  249.  
  250.     }
  251.  
  252.  
  253.  
  254.     public void surfaceDestroyed(SurfaceHolder holder) {
  255.  
  256.         // Surface will be destroyed when we return. Stop the preview.
  257.  
  258.         mHasSurface = false;
  259.  
  260.         pause();
  261.  
  262.     }
  263.  
  264.  
  265.  
  266.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  267.  
  268.         // Surface size or format has changed. This should not happen in this
  269.  
  270.         // example.
  271.  
  272.     }
  273.  
  274.  
  275.  
  276.     // ----------------------------------------------------------------------
  277.  
  278.  
  279.  
  280.     class PreviewThread extends Thread {
  281.  
  282.         private boolean mDone;
  283.  
  284.  
  285.  
  286.         PreviewThread() {
  287.  
  288.             super();
  289.  
  290.             mDone = false;
  291.  
  292.         }
  293.  
  294.  
  295.  
  296.         @Override
  297.  
  298.                 public void run() {
  299.  
  300.             // We first open the CameraDevice and configure it.
  301.  
  302.             CameraDevice camera = CameraDevice.open();
  303.  
  304.             if (camera != null) {
  305.  
  306.                 CameraDevice.CaptureParams param = new CameraDevice.CaptureParams();
  307.  
  308.                     param.type = 1; // preview
  309.  
  310.                     param.srcWidth      = 1280;
  311.  
  312.                     param.srcHeight     = 960;
  313.  
  314.                     param.leftPixel     = 0;
  315.  
  316.                     param.topPixel      = 0;
  317.  
  318.                     param.outputWidth   = 320;
  319.  
  320.                     param.outputHeight  = 240;
  321.  
  322.                     param.dataFormat    = 2; // RGB_565
  323.  
  324.                 camera.setCaptureParams(param);
  325.  
  326.             }
  327.  
  328.  
  329.  
  330.             // This is our main acquisition thread's loop, we go until
  331.  
  332.             // asked to quit.
  333.  
  334.             SurfaceHolder holder = mHolder;
  335.  
  336.             while (!mDone) {
  337.  
  338.                 // Lock the surface, this returns a Canvas that can
  339.  
  340.                 // be used to render into.
  341.  
  342.                 Canvas canvas = holder.lockCanvas();
  343.  
  344.  
  345.  
  346.                 // Capture directly into the Surface
  347.  
  348.                 if (camera != null) {
  349.  
  350.                     camera.capture(canvas);
  351.  
  352.                 }
  353.  
  354.  
  355.  
  356.                 // And finally unlock and post the surface.
  357.  
  358.                 holder.unlockCanvasAndPost(canvas);
  359.  
  360.             }
  361.  
  362.  
  363.  
  364.             // Make sure to release the CameraDevice
  365.  
  366.             if (camera != null)
  367.  
  368.                 camera.close();
  369.  
  370.         }
  371.  
  372.  
  373.  
  374.         public void requestExitAndWait() {
  375.  
  376.             // don't call this from PreviewThread thread or it a guaranteed
  377.  
  378.             // deadlock!
  379.  
  380.             mDone = true;
  381.  
  382.             try {
  383.  
  384.                 join();
  385.  
  386.             } catch (InterruptedException ex) { }
  387.  
  388.         }
  389.  
  390.     }
  391.  
  392. }
  393.  
  394.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4
bobalot
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 04, 2008 8:52 pm

Top

Re: SurfaceView on xml

Postby KrystalRe » Thu May 31, 2012 1:44 pm

Couldn't download your link. Can you reupload it?
KrystalRe
Once Poster
Once Poster
 
Posts: 1
Joined: Thu May 31, 2012 1:41 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests