SPRING :: NOTE

[ANDROID] SURFACEVIEW를 이용하여 CAMERA 사용하기 #4 - 촬영 기능 구현 본문

Development Language/JAVA · ANDROID

[ANDROID] SURFACEVIEW를 이용하여 CAMERA 사용하기 #4 - 촬영 기능 구현

RAYZIE 2017. 6. 19. 16:50
반응형

[ANDROID] SURFACEVIEW를 이용하여 CAMERA 사용하기 #1

[ANDROID] SURFACEVIEW를 이용하여 CAMERA 사용하기 #2

[ANDROID] SURFACEVIEW를 이용하여 CAMERA 사용하기 #3 - 오버레이 추가


AndroidCamera의 사진촬영 기능을 구현하려면 

ShutterCallback, RAW 데이터의 PictureCallback 및 JPG 데이터의 PictureCallback을 구현해야합니다. 

사진을 찍으려면 본 포스팅의 camera.takePicture() Method를 추가해야 합니다. 

본 포스팅은 takePicture() Method를 구현한 소스만 담고있으며, 이전 소스를 보려면 상단의 1,2,3 링크를 참고하시기 바랍니다.

구현 이미지

AndroidCamera.java

package com.exercise.AndroidCamera;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
      getWindow().setFormat(PixelFormat.UNKNOWN);
      surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
      surfaceHolder = surfaceView.getHolder();
      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
      controlInflater = LayoutInflater.from(getBaseContext());
      View viewControl = controlInflater.inflate(R.layout.control, null);
      LayoutParams layoutParamsControl
       = new LayoutParams(LayoutParams.FILL_PARENT,
       LayoutParams.FILL_PARENT);
      this.addContentView(viewControl, layoutParamsControl);
    
      Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
      buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   camera.takePicture(myShutterCallback,
     myPictureCallback_RAW, myPictureCallback_JPG);
  }});
  }

  ShutterCallback myShutterCallback = new ShutterCallback(){

 @Override
 public void onShutter() {
  // TODO Auto-generated method stub
 
 }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

 @Override
 public void onPictureTaken(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub
 
 }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

 @Override
 public void onPictureTaken(byte[] arg0, Camera arg1) {
  // TODO Auto-generated method stub
  Bitmap bitmapPicture
   = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
 }};

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
  int height) {
 // TODO Auto-generated method stub
 if(previewing){
  camera.stopPreview();
  previewing = false;
 }

 if (camera != null){
  try {
   camera.setPreviewDisplay(surfaceHolder);
   camera.startPreview();
   previewing = true;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
 // TODO Auto-generated method stub
 camera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
 // TODO Auto-generated method stub
 camera.stopPreview();
 camera.release();
 camera = null;
 previewing = false;
}
}

다음 - [ANDROID] SURFACEVIEW를 이용하여 CAMERA 사용하기 #5 - 촬영된 이미지 저장


반응형
Comments