Skip to content
Search

Android.hardware.Camera

lastmod 2018-08-17created 2016-06-073 edits

Notes on the Android camera.

To take pictures with this class, use the following steps

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
  4. If desired, call setDisplayOrientation(int).
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
  7. When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data.
  8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
  9. Call stopPreview() to stop updating the preview surface.
  10. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

To quickly switch to video recording mode, use these steps

  1. Obtain and initialize a Camera and start preview as described above.
  2. Call unlock() to allow the media process to access the camera.
  3. Pass the camera to setCamera(Camera). See MediaRecorder information about video recording.
  4. When finished recording, call reconnect() to re-acquire and re-lock the camera.
  5. If desired, restart preview and take more photos or videos.
  6. Call stopPreview() and release() as described above.

Capture camera preview and GLSurfaceView to a file

Bitmap bm;
MySurfaceViewImpl sv;
Canvas c = new Canvas(bm);
sv.draw(c);

When the Bitmap does not arrive in the callback function during camera control

static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
    final int frameSize = width * height;

    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (int i = 0; i < width; i++, yp++) {

            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0)
                y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0) r = 0; else if (r > 262143) r = 262143;
            if (g < 0) g = 0; else if (g > 262143) g = 262143;
            if (b < 0) b = 0; else if (b > 262143) b = 262143;

            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }
    }
}
// The int[] rgb buffer passed in should be at least width * height in length. 

What if the image is rotated when taking, saving, and loading photos with a Surface on the Android camera?

If you take the photo by calling the stock Android built-in program through an Intent, orientation information is added, so you only need to rotate based on that; but if you build it yourself with a surface and take the photo, there is no such information. In that case, rotate it when saving, as below. It works well on 2.2 and later.

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream outStream = null;
        try {
            imageFilePath = getFilename();
            InputStream is = new ByteArrayInputStream(data);
            Bitmap bmp = BitmapFactory.decodeStream(is);
            // Getting width & height of the given image.
            if (bmp != null) {
                int w = bmp.getWidth();
                int h = bmp.getHeight();
                // Setting post rotate to 90
                Matrix mtx = new Matrix();
                mtx.postRotate(90);
                // Rotating Bitmap
                Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                outStream = new FileOutputStream(String.format(imageFilePath, System.currentTimeMillis()));
                outStream.write(byteArray);
                outStream.close();
            } else {
                outStream = new FileOutputStream(String.format(imageFilePath, System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
            }

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

Making an Android live filter

A Live Filter is an image made by intercepting a Still Image midway and applying an effect to it, and displaying it on screen in real time.

  1. Making a live filter #4 What is the YUV Format: Android_camera_live_filter4.pdf
  2. Making a live filter #5 Implementing the Live Filter: Android_camera_live_filter5.pdf
  3. Making a live filter #6 Implementation (JAVA): Android_camera_live_filter6.pdf
  4. Making a live filter #7: Implementation (library - C/C++ implementation): Android_camera_live_filter7.pdf
  5. Making a live filter #8 Using OpenGLES 2.0: Android_camera_live_filter8.pdf

Example

See also

Favorite site

References


  1. Android_camera_preview.pdf ↩

  2. Android_opengl_preview_camera.pdf ↩

  3. Android_video_recorder.pdf ↩

  4. Android_video_recorder_for_mp4.pdf ↩

  5. Android_camera_image_save.pdf ↩

  6. Android_camera_control_basic.pdf ↩

  7. How_to_use_camera_view_with_opengles.pdf ↩

  8. Android_camera_picture_size_select.pdf ↩