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
- Obtain an instance of Camera from open(int).
- Get existing (default) settings with getParameters().
- If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
- If desired, call setDisplayOrientation(int).
- Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
- Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
- 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.
- After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
- Call stopPreview() to stop updating the preview surface.
- 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
- Obtain and initialize a Camera and start preview as described above.
- Call unlock() to allow the media process to access the camera.
- Pass the camera to setCamera(Camera). See MediaRecorder information about video recording.
- When finished recording, call reconnect() to re-acquire and re-lock the camera.
- If desired, restart preview and take more photos or videos.
- Call stopPreview() and release() as described above.
Capture camera preview and GLSurfaceView to a file
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.
- Making a live filter #4 What is the YUV Format: Android_camera_live_filter4.pdf
- Making a live filter #5 Implementing the Live Filter: Android_camera_live_filter5.pdf
- Making a live filter #6 Implementation (JAVA): Android_camera_live_filter6.pdf
- Making a live filter #7: Implementation (library - C/C++ implementation): Android_camera_live_filter7.pdf
- Making a live filter #8 Using OpenGLES 2.0: Android_camera_live_filter8.pdf
Example
- android.hardware.Camera:OpenGLES: How to show a camera preview using GLSurfaceView on Android.
- android.hardware.Camera:Save: How to take and save a photo on Android.
- android.hardware.Camera:Preview: A simple Activity that displays the camera preview on Android.
See also
Favorite site
- Developer.android.com CAMERA
- How to set up the Android camera preview 1
- [Recommended] Render camera preview using OpenGL ES 2.0 on Android 2
- Full video source analysis. Explanation of how to record video 3
- Android video recording example
- Example of recording video (mp4) on an Android phone 4
- Android NV21 Bitmap conversion
- Android developer ImageFormat.NV21
- YUV420 coming from the android camera
- Android JNI/NDK Camera (YUV420 to RGB, RGBA)
- Android YUV420 Format
- Taking a photo with the camera, getting the Bitmap, and showing it in an ImageView 5
- How to take and save a photo on Android
- Basics of Android camera control 6
- How to use camera view with OpenGLES 7
- Guidelines for camera preview using OpenGL
- Showing the Camera Preview in Opengl on Android. (about the NV21 format)
- Kunny's Android: Let's use the camera! Understanding SurfaceView
- How to choose the Android Camera Picture Size 8
- [Recommended] Understanding SurfaceView and showing a CameraView
- [Recommended] Processing images in the NDK layer and returning them (Image Processing in NDK)
- How to extract rgb colors from Android camera photos
- Camera Preview is not FULL SCREEN in android
- Capture only that part of the camera preview(decodeYUV)
- Optimizing the Android camera preview fit
- Android camera preview NV21 Bitmap conversion
- Images received from a smartphone camera
- Displaying the Android Surface Camera Preview
- Showing the Camera Preview in Opengl on Android
- Converting Android camera image data
- Camera updates changed in Android 1.6 2.0 2.1 ~ versions 1.5 and later
- Stackoverflow: Android Camera without Preview
- Stackoverflow: Android SDK - Get raw preview camera image without displaying it
- [Recommended] Render camera preview using OpenGL ES 2.0 on Android
- Taking photos and camera preview: main steps of the camera preview
- [Recommended] Hello world - Integrating the default camera app on Android