I was trying to write a simple camera app, but I ran into quite a frustrating problem with my picture preview. It shows horizontal flicker lines running up the screen. My screen has a width of 800 (Galaxy SII plus), so there are black spaces on the edges (the activity is set to horizontal orientation). Now these black margins begin to transition to a white-grey at the bottom ends when the flicker occurs, so I suspect there might be something drawn over them.
The really annoying part is that this does not seem to depend on the actual code, since it sometimes works without any flickering.
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera camera;
private Point size;
Preview(Context context, Camera camera, Point size) {
super(context);
this.camera = camera;
this.size = size;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setWillNotDraw(false);
Parameters parameters = camera.getParameters();
parameters.setPreviewSize(720, 480);
parameters.setPictureSize(720, 480);
camera.setParameters(parameters);
Camera.Size previewSize = camera.getParameters().getPreviewSize();
setLayoutParams(new FrameLayout.LayoutParams(previewSize.width,
previewSize.height, Gravity.CENTER));
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null) {
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
}
Camera.Size previewSize = camera.getParameters().getPreviewSize();
setLayoutParams(new FrameLayout.LayoutParams(previewSize.width,
previewSize.height, Gravity.CENTER));
try {
camera.setPreviewDisplay(mHolder);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
public void setCamera(Camera camera) {
if (this.camera == camera) {
return;
}
stopPreviewAndFreeCamera();
this.camera = camera;
if (this.camera != null) {
requestLayout();
try {
this.camera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
this.camera.startPreview();
}
}
public void stopPreviewAndFreeCamera() {
if (this.camera != null) {
this.camera.stopPreview();
this.camera.release();
this.camera = null;
}
}
public void takePicture() {
camera.autoFocus(null);
camera.takePicture(null, null, new PictureHandler(this.getContext()));
camera.startPreview();
}
}
public class MainActivity extends Activity {
private Preview mPreview;
.
.
.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
this.mPreview.stopPreviewAndFreeCamera();
finish();
}
return super.onKeyDown(keyCode, event);
}
}
Related
I have a simple class called CameraPreview, this is the code:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
} catch (RuntimeException e) {}
try {
SurfaceHolder someHolder = holder;
mCamera.setPreviewDisplay(someHolder);
mCamera.startPreview();
} catch (IOException e) {
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null)
return;
try {
mCamera.stopPreview();
} catch (Exception e){}
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){}
}
}
It's obvious that, while the application is showing the CameraPreview on screen, it will consume battery faster, my question is,what will happen to the battery if I set this view to View.GONE?
According to this SO answer setting the SurfaceView visbility to GONE will call surfaceDestroyed method.
Therefore, since you are stopping the camera, you won't consume user's battery.
I am using following code to turn on camera flashlight
public Flashlight(SurfaceView preview, Context context){
this.preview = preview;
this.context = context;
mHolder = preview.getHolder();
mHolder.addCallback(this);
mCamera = Camera.open();
cameraOpened = true;
try {
mCamera.setPreviewDisplay(mHolder);
}catch (Exception e){
e.printStackTrace();
}
params = mCamera.getParameters();
mCamera.startPreview();
}
private void turnOnFlashlight(){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
}
private void turnOffFlashlight(){
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mHolder = holder;
try {
mCamera.setPreviewDisplay(mHolder);
}catch(Exception e){
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mHolder = null;
}
it uses surface view and it works great on many phones however I cant get it to work on galaxy S3. Have you experienced similary problems? Any ideas how to get it working on S3?
Thanks in forward
Having a little trouble getting my SurfaceView to show my camera preview. I've looked at some questions on here and Google'd some tuts but I think it may be a small error on my end that I'm just not seeing.
Code
public class RoofPitchActivity extends Activity implements SensorEventListener {
...
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roof_pitch);
initViews();
}
private void initViews() {
...
Preview preview = new Preview(this);
mCamera = Camera.open();
preview.setCamera(mCamera);
}
...
...
class Preview extends ViewGroup implements SurfaceHolder.Callback {
public Preview(Context context) {
super(context);
mSurfaceView = (SurfaceView) findViewById(R.id.preview);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
}
public void setCamera(Camera camera) {
if (mCamera == camera) {
return;
}
stopPreviewAndFreeCamera();
mCamera = camera;
if (mCamera != null) {
requestLayout();
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
private void stopPreviewAndFreeCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
}
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
}
So when the activity is launched the SurfaceView is black. The camera does not appear to be rendering the preview onto the SurfaceView. I'm sure there's something small I'm missing, or maybe it's just a fundemental misunderstanding of how this works. A fresh set of eyes with a little explanation would be very much appreciated. Thanks
So the answer to my question ended up being device specific. The code was acceptable but my device I'm using to debug is a Nexus 7. This means the camera is front facing and the normal call to the camera does not work. An interesting and funny read about this can be found in this article
Camera.open();
does not work. You must use
Camera.open(0);
So my code ended up with a conditional check for the camera and now it works fine.
Like so
public void setCamera() {
stopPreviewAndFreeCamera();
PackageManager pm = getPackageManager();
boolean backCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
boolean frontCamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
if (frontCamera) {
mCamera = Camera.open(0);
} else if (backCamera) {
mCamera = Camera.open();
}
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mSurfaceView.getWidth(), mSurfaceView.getHeight());
requestLayout();
try {
mCamera.setParameters(parameters);
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
I am making an app for reading small text but i am not able to get sharp focus . i am using SCENE_MODE_BARCODE for scene mode for small text but it seems to be not working i tried autofocus also but no result either.
sorry for bad english.
my preview class code is
public class MyView extends SurfaceView implements SurfaceHolder.Callback {
Camera surfacecmr;
SurfaceHolder mholder;
#SuppressWarnings("deprecation")
public MyView(Context context, Camera cmr) {
super(context);
surfacecmr = cmr;
mholder = getHolder();
mholder.addCallback(this);
mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
// surfacecmr.setDisplayOrientation(90);
surfacecmr.setPreviewDisplay(holder);
surfacecmr.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (mholder.getSurface() == null)
return;
surfacecmr.stopPreview();
// get Camera parameters
Camera.Parameters params = surfacecmr.getParameters();
params.setSceneMode(Camera.Parameters.SCENE_MODE_BARCODE);
surfacecmr.setParameters(params);
try {
surfacecmr.setPreviewDisplay(mholder);
} catch (IOException e) {
e.printStackTrace();
}
surfacecmr.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
surfacecmr.release();
}
}
public class PreviewDemo extends Activity {
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
private boolean inPreview=false;
private boolean cameraConfigured=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preview=(SurfaceView)findViewById(R.id.preview);
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void onResume() {
super.onResume();
camera=Camera.open();
startPreview();
}
#Override
public void onPause() {
if (inPreview) {
camera.stopPreview();
}
camera.release();
camera=null;
inPreview=false;
super.onPause();
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters parameters) {
Camera.Size result=null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if (size.width<=width && size.height<=height) {
if (result==null) {
result=size;
}
else {
int resultArea=result.width*result.height;
int newArea=size.width*size.height;
if (newArea>resultArea) {
result=size;
}
}
}
}
return(result);
}
private void initPreview(int width, int height) {
if (camera!=null && previewHolder.getSurface()!=null) {
try {
camera.setPreviewDisplay(previewHolder);
}
catch (Throwable t) {
Log.e("PreviewDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast
.makeText(PreviewDemo.this, t.getMessage(), Toast.LENGTH_LONG)
.show();
}
if (!cameraConfigured) {
Camera.Parameters parameters=camera.getParameters();
Camera.Size size=getBestPreviewSize(width, height,
parameters);
if (size!=null) {
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
cameraConfigured=true;
}
}
}
}
private void startPreview() {
if (cameraConfigured && camera!=null) {
camera.startPreview();
inPreview=true;
}
}
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
// no-op -- wait until surfaceChanged()
}
public void surfaceChanged(SurfaceHolder holder,
int format, int width,
int height) {
initPreview(width, height);
startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// no-op
}
};
}
The above code is working fine for when i launch the application the camera preview is displayed with no actions performed in that. But i want to get an image overlay on the live feed camera. please help me.....
I will give you a hint, if you are still stuck. In your layout definition you can define a frame layout or a relative layout in a way that the views are or apear to be on top of each other. You can see an example here:
http://littlesaiph.blogspot.de/2011/07/getting-images-laid-pause-over-video-in.html