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?
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="" />