내용 보기

작성자

관리자 (IP : 172.17.0.1)

날짜

2020-07-09 07:53

제목

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


AndroidCamera의 사진촬영 기능을 구현하려면 
ShutterCallback, RAW 데이터의 PictureCallback 및 JPG 데이터의 PictureCallback을 구현해야합니다. 
사진을 찍으려면 본 포스팅의 camera.takePicture() Method를 추가해야 합니다. 
본 포스팅은 takePicture() Method를 구현한 소스만 담고있습니다.

구현 이미지



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 formatint 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;
}
}
cs


출처1

https://argc.tistory.com/239?category=699460

출처2