Camera blank/black in landscape view? Droid 2.0.1

All your problems with Audio, Video and Images.

Camera blank/black in landscape view? Droid 2.0.1

Postby jerrygarciame » Wed Mar 31, 2010 1:31 am

I can get the camera to preview in portrait, but when I force landscape, the view is blank/black? Any suggestions would be very much appreciated.

public class cameraActivity extends Activity {

Camera camera;
cameraView preview;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.camera_surface);

preview = new cameraView(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);


}


}

----
public class cameraView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;

public cameraView(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (camera==null) camera = Camera.open();

Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);

camera.setParameters(parameters);

try {
camera.setPreviewDisplay(mHolder);

} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;

}

@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p = new Paint(Color.RED);
Log.d(TAG, "draw");
canvas.drawText("PREVIEW", canvas.getWidth() / 2,
canvas.getHeight() / 2, p);
}

}
jerrygarciame
Freshman
Freshman
 
Posts: 9
Joined: Tue Mar 30, 2010 4:25 am

Top

This works!

Postby jerrygarciame » Wed Mar 31, 2010 2:05 am

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class DefaultActivity extends Activity {

MySurfaceView surface = null;
public Camera camera;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

surface = new MySurfaceView(this);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(surface);
}

/**
* Surface View
*/
class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holder;

public MySurfaceView(Context context) {
super(context);

holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.set("orientation", "portrait");
parameters.setRotation(90);
camera.setParameters(parameters);
}

public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();


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

public void surfaceDestroyed(SurfaceHolder holder) {

if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
}
}
jerrygarciame
Freshman
Freshman
 
Posts: 9
Joined: Tue Mar 30, 2010 4:25 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests