androidカスタムカメラで写真を撮って写真を見ます。


本論文の例では、Androidカスタムカメラの写真を共有し、画像の具体的なコードを確認します。参考にしてください。具体的な内容は以下の通りです。
1、カメラを開く
a.写真のプレビューはSurfaceViewに必要であり、そのコールバック関数implemens SurfaceHolder.Callbackを実現する。
activitycamera.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <SurfaceView
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

  <Button
    android:id="@+id/btn_camera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="  " />

</LinearLayout>
2、カメラを取得してプレビューを開く

/**
   *       
   * 
   * @return
   */
  private Camera getcCamera() {
    Camera camera = null;
    try {
      camera = Camera.open();
    } catch (Exception e) {
      camera = null;
    }
    return camera;
  }


/**
   *         
   * 
   * @return
   */
  private void showCameraView(Camera camera,SurfaceHolder holder)
  {
    try {
      camera.setPreviewDisplay(holder);
      camera.setDisplayOrientation(90);
      camera.startPreview();
    }catch (Exception e){
      e.printStackTrace();
    };

  }

/**
**         
*/
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    showCameraView(mCamera, holder);
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    mCamera.stopPreview();
    showCameraView(mCamera, holder);
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    clearCamera();
  }
}

3、カメラのホームページ処理

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback{

  private SurfaceView sv;
  private Camera mCamera;
  private SurfaceHolder holder;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    sv = (SurfaceView)findViewById(R.id.sv);
    holder = sv.getHolder();
    holder.addCallback(this);

    findViewById(R.id.btn_camera).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //         
        Camera.Parameters parameters = mCamera.getParameters();
        //       
        parameters.setPictureFormat(ImageFormat.JPEG);
        //       
        parameters.setPreviewSize(800, 480);
        //       ,        
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        mCamera.autoFocus(new Camera.AutoFocusCallback() {

          @Override
          public void onAutoFocus(boolean success, Camera camera) {
            //         
            if (success) {
              //              
              mCamera.takePicture(null, null, new Camera.PictureCallback() {
                @Override
                public void onPictureTaken(byte[] data, Camera camera) {
                  // data     
                  File file = new File("/sdcard/photo.png");
                  //        
                  try {
                    FileOutputStream fos = new FileOutputStream(file);
                    try {
                      fos.write(data);
                      //    
                      fos.close();
                      //     
                      Intent intent = new Intent();
                      //     
                      intent.putExtra("path", file.getAbsolutePath());
                      setResult(0,intent);
                      finish();
                    } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                    }
                  } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  }
                }
              });
            }
          }
        });
      }
    });

  }


 @Override
  protected void onResume() {
    super.onResume();
    if (mCamera == null) {
      mCamera = getCamera();
      if (holder != null) {
        showCameraView(mCamera, holder);
      }
    }
  }
  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    // activity           
    clearCamera();

  }
  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  /**
   *        
   */
  private void clearCamera() {

    //   hold  
    if (mCamera != null) {
      //     
      mCamera.stopPreview();
      mCamera.setPreviewCallback(null);
      //       
      mCamera.release();
      mCamera = null;
    }
  }

4、activityを起動する(写真を撮った後のパスを設定して表示画像処理に戻り、必ず写真をサンプリングレートで操作する(写真が多すぎて表示できないかもしれないし、また何の異常も報告しない)

public class MainActivity extends AppCompatActivity {

  private ImageView cameraIv;

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == 0 && requestCode == 100)
    {
      String path = data.getStringExtra("path");
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, options);
      ImageSize imageSize = getImageViewSize(cameraIv);
      options.inSampleSize = caculateInSampleSize(options,
          imageSize.width, imageSize.height);

      //       InSampleSize      
      options.inJustDecodeBounds = false;
      Bitmap bitmap = BitmapFactory.decodeFile(path, options);
      cameraIv.setImageBitmap(bitmap);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button openButton = (Button) findViewById(R.id.button);
    cameraIv = (ImageView) findViewById(R.id.imageView);



    openButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this,CameraActivity.class);
        startActivityForResult(intent,100);
      }
    });
  }

   /*                     SampleSize
   *
       * @param options
   * @param width
   * @param height
   * @return
       */
  public static int caculateInSampleSize(BitmapFactory.Options options, int reqWidth,
                      int reqHeight)
  {
    int width = options.outWidth;
    int height = options.outHeight;

    int inSampleSize = 1;

    if (width > reqWidth || height > reqHeight)
    {
      int widthRadio = Math.round(width * 1.0f / reqWidth);
      int heightRadio = Math.round(height * 1.0f / reqHeight);

      inSampleSize = Math.max(widthRadio, heightRadio);
    }

    return inSampleSize;
  }

  public static ImageSize getImageViewSize(ImageView imageView)
  {

    ImageSize imageSize = new ImageSize();
    DisplayMetrics displayMetrics = imageView.getContext().getResources()
        .getDisplayMetrics();

    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)imageView.getLayoutParams();

    int width = imageView.getWidth();//   imageview     
    if (width <= 0)
    {
      width = lp.width;//   imageview layout      
    }

    if (width <= 0)
    {
      width = displayMetrics.widthPixels;
    }

    int height = imageView.getHeight();//   imageview     
    if (height <= 0)
    {
      height = lp.height;//   imageview layout      
    }

    if (height <= 0)
    {
      height = displayMetrics.heightPixels;
    }
    imageSize.width = width;
    imageSize.height = height;

    return imageSize;
  }

  public static class ImageSize
  {
    int width;
    int height;
  }
}

以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。