88870

“Error 1” occurring during camera overlay in Android

Question:

I have created an Activity that has a SurfaceView and a SurfaceHolder in order to create a Camera overlay. For some reason, I am getting the following in ADB: <strong>04-08 10:54:06.747: E/Camera(1152): Error 1</strong>. I am able to take a picture, but I cannot get any kind of preview. Here is the code I am using:

public class MyActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback, OnClickListener { private Camera mCamera; private SurfaceView videoPreview; private SurfaceHolder surfaceHolder; private Button cancel; private Button takePicture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.camera_layout); videoPreview = (SurfaceView) findViewById(R.id.video_preview); cancel = (Button) findViewById(R.id.cancel); cancel.setOnClickListener(this); takePicture = (Button) findViewById(R.id.take_picture); takePicture.setOnClickListener(this); surfaceHolder = videoPreview.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Camera.open(getNumberOfCameras() - 1); => access front facing camera as well mCamera = Camera.open(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Camera.Parameters parameters = mCamera.getParameters(); // pick the device's largest supported camera size List<Camera.Size> cameraSizes = parameters.getSupportedPreviewSizes(); // the first size is the largest Camera.Size selectedSize = cameraSizes.get(0); parameters.setPreviewSize(selectedSize.width, selectedSize.height); mCamera.setDisplayOrientation(90); mCamera.setParameters(parameters); mCamera.startPreview(); } @Override public void surfaceCreated(SurfaceHolder holder) { try { mCamera.setPreviewDisplay(surfaceHolder); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override protected void onPause() { super.onPause(); mCamera.stopPreview(); } @Override protected void onDestroy() { super.onDestroy(); mCamera.release(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.cancel: finish(); break; case R.id.take_picture: mCamera.takePicture(this, null, null, this); break; } } @Override public void onPictureTaken(byte[] data, Camera camera) { // store the picture FileOutputStream fos = null; File imageFile = null; try { imageFile = new File(Environment.getExternalStorageDirectory(), "custom_image1.png"); fos = new FileOutputStream(imageFile); fos.write(data); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.flush(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } mCamera.startPreview(); } @Override public void onShutter() { // perform an animation }

}

And here is the layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <SurfaceView android:layout_weight="1" android:id="@+id/video_preview" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/cancel" android:layout_width="100dip" android:layout_height="wrap_content" android:text="Cancel" />  <Button android:id="@+id/take_picture" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="Snap Photo" /> </LinearLayout>

I have tried changing this to RelativeLayout but to no avail. I really think I have just missed something simple. Any help is greatly appreciated. Thanks!

Answer1:

You have no onResume() method to call startPreview() again, hence when you take a picture, the preview is not running, so the capture fails.

To test this, try adding camera.startPreview() right before you capture an image, and see if that works.

Recommend

  • Releasing camera instance after a single image is captured and returning to parent activity
  • Sending email through intent without having to press send button
  • How to centrally define IComparable on abstract (interface) types in F#
  • CropActivity is not starting in onActivityResult inside Fragment
  • iphone page curl without uiviewanimation
  • TrackballControls.js: this.domElement.addEventListener is not a function
  • Swift observer pattern when photo library changes
  • How to capture **Video and Photo** at same time in an activity
  • How to remove the background from static image in Java using OpenCV?
  • How calculate screen position
  • OpenCV: wrap image to cylindrical coordinates
  • Going back and forth between intents without losing user entered data
  • Android TV Emulator: No Internet Access
  • How to change color of Camera LED / Flashlight in Android
  • Activate Two Cameras At The Same Time
  • drawRect Doesn't want to draw anything
  • Opening video with openCV +python
  • How can I write a file to a folder of the internal storage on Android?
  • EffectComposer second pass “overwrites” first pass
  • SDK Error: 0x8D07, while using Canon SDK in C#
  • Changing two button into a toggle button
  • Save pictures to custom folder in media library
  • Rendering complete camera view(16:9) onto a texture in Unity3d
  • Resource for understanding view, projection, 'virtual camera' in OpenGL or graphics in gen
  • Unable to scan Code 128
  • How to move again MainActivity after sending the mail?
  • Phonegap GA plugin not working
  • Galaxy S4 Android Custom Camera Issue
  • jQuery Orbit - How to make a Random Slideshow?
  • Bad request using file_get_contents for PUT request in PHP
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?