1

For some strange reason, the surfaceView is displaying well the image but the red and blue channels are swapped. The code I'm using is basically the same as in Github project but with some minor changes.

The code as I'm using it is:

public class CameraView extends SurfaceView implements SurfaceHolder.Callback{

private SurfaceHolder surfaceHolder = null;
private Camera camera = null;

public CameraView(Context context) {
    super(context);
    surfaceHolder = this.getHolder();
    surfaceHolder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    Parameters parameters = camera.getParameters();
    parameters.setPreviewFpsRange(30000, 30000);            
    camera.setParameters(parameters);   
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    if (camera != null){                    // Start the preview for surfaceChanged
        try {
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            this.releaseCamera();
        }
        camera.startPreview();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    this.releaseCamera();                   // Do not hold the camera during surfaceDestroyed - view should be gone...
}

public void releaseCamera() {
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

}

Any ideas why this color swapping?

gkapellmann
  • 313
  • 3
  • 19
  • 1
    Didn't you consider opening an _[issue](https://github.com/jaredsburrows/OpenQuartz/issues/new)_ on GitHub? – Alex Cohn Jul 09 '14 at 18:43
  • Hi Alex, well also I found no answer in here and eventually someone will have the same or similar problem, and an answer to this could be useful. Up to now, I've search and tested many solutions with no success. Also I'm no Android expert. – gkapellmann Jul 10 '14 at 13:02
  • I did not say "look for answers on GitHub". But maybe of the people who follow the OpenQuartz project (32 right now) some have an answer, or can find an answer, but are not subscribed to SO **[[google-glass](http://stackoverflow.com/tags/google-glass/info)]** – Alex Cohn Jul 10 '14 at 13:17
  • Ok, good point, I'll follow your advice. – gkapellmann Jul 10 '14 at 13:24

5 Answers5

6

I had the same problem, it drove me crazy. After a lot of efforts I found out that for some strange reason this happening when the layout contains only the FrameLayout which holds the SurfaceView of the camera and no other layout elements. In my case I added empty TextView over the FrameLayout and the it resolved. Strange but working. (eventually I used this TextView to show text so this is the reason for the style and location attributes)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${relativePackage}.${activityClass}"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/CameraView" />

 <TextView
    android:id="@+id/status_text"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dip"
    android:layout_gravity="center_horizontal|top"

    android:padding="5dip"
    android:textColor="#000000"

    android:text="" />

nel
  • 78
  • 1
  • 4
2

I'm having the same problem. A workaround I found was to put a margin on the screen.

dimens.xml

  <dimen name="glass_video_margin">1px</dimen>

layout_activity.xml

    <VideoView
    android:id="@+id/vid_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/glass_video_margin"
     />
Euber
  • 33
  • 5
  • And you load this layout just as a ContentView? – gkapellmann Sep 29 '14 at 12:38
  • Yes, this is just part of layout. I associate this VideoView in screen with findViewById. This example is for play a video. In your case you probably need to change the VideoView to Surface View. – Euber Sep 30 '14 at 14:24
  • @Euber Seriously this was bugging the crap out of me. Added a 40px margin and it did the trick! THANK YOU – Zack Feb 01 '15 at 09:52
0

Are you setting the ContentView of your activity to be the CameraView? If so, this issue will occur. I was facing the same problem with the below code.

@Override
protected void onResume() {
    super.onResume();
    cameraPreview = new CameraPreview(this);
    // setUpCameraPreview();
    setContentView(cameraPreview);
}

Later I created activity_main.xml with the following.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    tools:context="com.arun.camera.CustomCamera">

    <FrameLayout
        android:id="@+id/cameraPreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

Then I created the camera preview in my onCreate as follows, and so things are just fine. My onCreate now looks as follows.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    previewLayout = (FrameLayout) this.findViewById(R.id.cameraPreview);
    cameraPreview = new CameraPreview(this);
    previewLayout.addView(cameraPreview);
}
Arun
  • 1,399
  • 12
  • 21
  • Super! I´ll try it and let you know! – gkapellmann Aug 14 '14 at 09:13
  • Actually I,ve been fighting yesterday with this, for some reason (I wanted to have more info to answer you) my screen just freezes and the complete GGlass up to the point I need to hard reset. – gkapellmann Aug 15 '14 at 11:35
  • May be you are doing too much work on your main (UI) thread. It is also possible that you are not starting and stoping the camera preview in the right way. – Arun Aug 15 '14 at 17:01
  • So, I made it work, but the colours are still swapped. I think that the problems comes directly from 'cameraPreview' object, rather than from the contentview. – gkapellmann Aug 18 '14 at 13:32
0

I had the same problem, but not only on camera video preview. I resolved it. I added android:hardwareAccelerated="true" into androidmanifest.xml activity section. Now works fine. (Sorry my bad english.)

-1

This question may be related:

Glass camera preview display is garbled

Community
  • 1
  • 1
lily
  • 59
  • 2
  • 2
  • 1
    He has clearly mentioned that the image preview is displayed well, but only the R and B channels are swapped. So the answer is not related. – Arun Aug 14 '14 at 17:06