Skip to content

Android.hardware.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);

카메라 제어에서 콜백함수로 Bitmap 이 안들어 올때

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. 

Android 카메라에서 Surface로 사진찍고 저장 및 불러올 때 화면 돌아가있다면?

일반 안드로이드에 내장된 프로그램으로 Intent 호출하여 찍는다면 orientation 정보가 추가되어있어 그것만 보고 돌리면 된다만 surface로 직접 만들어 찍으면 정보가 없다. 그럴땐 저장할 때 돌려주자 아래 처럼 2.2 이상에서 잘된다.

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 {
        }
    }
};

Android 라이브 필터 만들기

Live Filter란 Still Image를 중간에 가로채서 효과를 적용 시킨 이미지로서, 이를 실시간으로 화면에 보여주는 것을 말한다.

  1. 라이브 필터 만들기 #4 YUV Format 이란: Android_camera_live_filter4.pdf
  2. 라이브 필터 만들기 #5 Live Filter 구현하기: Android_camera_live_filter5.pdf
  3. 라이브 필터 만들기 #6 구현 (JAVA): Android_camera_live_filter6.pdf
  4. 라이브 필터 만들기 #7:구현 (library - C/C++ 구현): Android_camera_live_filter7.pdf
  5. 라이브 필터 만들기 #8 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