views:

2262

answers:

9

Hi, I'm new at this (java and opengl) so please bear with me if the answer to the question is simple. :)

I'm trying to get a camera preview screen with the ability to display 3d objects simultaneously. Having gone through the samples at the api demos, I thought combining the code for the the examples at the api demo would suffice. But somehow its not working. The forces me to shut down upon startup and the error is mentioned as null pointer exception. Could someone share with me where did I go wrong and how to proceed from there. How I did the combination for the code is as shown below:

myoverview.xml


<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <android.opengl.GLSurfaceView 
            android:id="@+id/cubes" 
            android:orientation="horizontal" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/> 
    <SurfaceView 
            android:id="@+id/camera" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/> 
</FrameLayout>

myoverview.java


import android.app.Activity; 
import android.os.Bundle; 
import android.view.SurfaceView; 
import android.view.Window; 
public class MyOverView extends Activity { 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       // Hide the window title. 
       requestWindowFeature(Window.FEATURE_NO_TITLE); 
       // camera view as the background 
       SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera); 
       cameraView = new CameraView(this); 
       // visual of both cubes 
       GLSurfaceView cubesView = (GLSurfaceView) findViewById(R.id.cubes); 
       cubesView = new GLSurfaceView(this); 
       cubesView.setRenderer(new CubeRenderer(false)); 
       // set view 
       setContentView(R.layout.myoverview); 
    } 
}

GLSurfaceView.java


import android.content.Context; 
class GLSurfaceView extends android.opengl.GLSurfaceView { 
    public GLSurfaceView(Context context) { 
            super(context); 
    } 
} 

NOTE :

  • I didnt list the rest of the files as they are just copies of the api demos. The cameraView refers to the camerapreview.java example and the CubeRenderer refers to the CubeRenderer.java and Cube.java example. Any help would be appreciated as I've been stuck at this for a couple of days :p Thanks

  • Sorry, didnt realise that the coding was out of place due to formatting mistakes. :p

A: 

Found out how to solve it... via the java way... just use addContentView instead of using xml.... well at least its solved. :)

pohtzeyun
A: 

Can I have the detail of your solution? I tried the method you used. The Java way. Somehow I must be wrong. Hope you can help me with this. I added the mediaplayer on bgView and it works well on its own. Either "setContentView(bgView..." or "addContentView(bgView..." works. the GLSurfaceView part is from ApiDemos.

        bgView = new SurfaceView(this);
        addContentView(bgView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        holder = bgView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    
        holder.setFormat(PixelFormat.TRANSLUCENT|LayoutParams.FLAG_BLUR_BEHIND);

        glView = new GLSurfaceView(this);
        addContentView(glView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        glView.setRenderer(new CubeRenderer(true));
bgView = new SurfaceView(this);holder = bgView.getHolder();holder.addCallback(this);holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.setFormat(PixelFormat.TRANSLUCENT|LayoutParams.FLAG_BLUR_BEHIND);glView = new GLSurfaceView(this);glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);glView.setRenderer(new CubeRenderer(true));addContentView(glView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));addContentView(bgView, params);NOTE: I'm not sure abt the first part but ur 2nd part is similar to mine so hope this helps. :)
pohtzeyun
+1  A: 

the reason you are getting a null pointer exception when working with .xml is because ur actually creating new Views in your java code.. instead of using the ones from the .xml file to which you might have passed in properties(if u did pass in properties that is..).. the new View would obviously have a null value.. thus throwing a null pointer exception... for example --

cubesView = new GLSurfaceView(this);

is actually not needed in the code if you already created the View in the .xml file containing FrameLayout..

bala singareddy
Thanks for pointing that out! :D
pohtzeyun
A: 

Dear Pohtzeyun did You solve your problem,if You know the answer please help me.

Marta
Hi Marta, What do you need? Will do my best to help if I can. :)
pohtzeyun
A: 

Hi Pohtzeyun.My code work without exception ,but I don't get the result as I want. I have the same xml, CubeRenderer.java and Cube.java files. Below is shown the part of my code:

    mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
    mSurfaceView.setOnClickListener(this);
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
    cubesView = (GLSurfaceView) findViewById(R.id.cubes);
    mcubrender = new CubeRenderer();
    cubesView.setRenderer(mcubrender);
    cubesView.setOnTouchListener(new OnTouchListener(){

        public boolean onTouch(View v, MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
            switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
                mcubrender.mAngleX += dx * TOUCH_SCALE_FACTOR;
                mcubrender.mAngleY += dy * TOUCH_SCALE_FACTOR;
                cubesView.requestRender();

            }
            mPreviousX = x;
            mPreviousY = y;
            return true;
        }

    });

I want to roatate the cube, but onTouch() method doesn't work.

When I change color of the cube ( gl.glColor4f(0.0f,0.0f,0.0f,0.0f)) I see black cube, but I want to have transparent cube, under which I can see my webcamera result.(The screen of my emulator shows the cube instead of my webcamera result.)

Thank you in advance.

Marta
Hi Marta, I'm not sure how to do what you have just mentioned for sure but these are my suggestions, hope they work. :) To rotate the cube, you need to modify the degree variable in the CubeRenderer.java file, near the section of the code as shown below: gl.glRotatef(mAngle*2.0f, 0, 1, 1); gl.glTranslatef(0.5f, 0.5f, 0.5f); mCube.draw(gl); update the variable(mAngle) and the mCube.draw(gl) will take care of rotating the cube for you. What I did was to increase the mAngle using a loop and it was able to work.
pohtzeyun
Sorry about the formatting for the previous comment, was not able to get the code block to work. :p Anyway, for the transparent/translucent part, you might want to try something like gl.glColor4F(1.0f, 0.0f, 0.0f, 0.0f). The format for glColor4F is RGBA so set only the last part (alpha) to make the background transparent/translucent. Then after this, make sure you are setting the layers in xml in this order from top to bottom <?xml version="1.0" encoding="utf-8"?> <RelativeLayout .... > <android.opengl.GLSurfaceView .... /> <com.owncameralayer.Camera .... /> </RelativeLayout>
pohtzeyun
cont... android.opengl.GLSurfaceView is the 3d cube class and com.owncameralayer.Camera is my camera class. After the xml part has been done, you will need to add this `yourGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);` to get the system to choose a format that supports transparency. That should be it.It worked for me and I was able to get a self rotating cube with a camera preview.I was not able to get the code formatting to work and if you want, you can email to me and I will type out the blocks again just to make things clearer. Sorry for the formatting problem. :D
pohtzeyun
Thank You very much Pohtzeyun for suggestion.
Marta
A: 

I am new to opengl. Please help me. I want to draw or put images into 3d transparent cube .I use the same CubeRender and Cube java classes from Api Demos.

David
A: 

qweqweqwe Hi ,,,

David
Hi David, sorry for the late reply. I havent tried that part yet as I've been busy with my own game. I do know that there are tutorials on the net that does teach u wat u wanna know. Might wanna goto anddev.org to take a look. Thanks and sorry for any inconvenience caused.
pohtzeyun
A: 

Hi Marta,

         I actually did that here in this link u can see the complete implementation.. 

http://stackoverflow.com/questions/3548666/overlay-images-onto-camera-preview- surfaceview/3867317#3867317

bala singareddy
A: 

This is very simple actually...if you want to define your view in XML you just have to implement

Public GLSurfaceView(Context context, AttributeSet attrs) {
...
super(context, attrs);
}

instead of GLSurfaceView(Context context)

That's the one that gets called automatically when the view is initialized from the XML. I had the same problem and that's how it was fixed.

Jlam