亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android自定義Camera拍照并查看圖片

 更新時間:2017年06月30日 16:59:23   作者:yaonga  
這篇文章主要為大家詳細介紹了android自定義Camera拍照并查看圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android自定義Camera拍照并查看圖片的具體代碼,供大家參考,具體內容如下

1、打開相機

a.預覽拍攝圖片,需用到SurfaceView,并且實現(xiàn)其回調函數(shù)implements SurfaceHolder.Callback;
activity_camera.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、獲取相機并開啟預覽

/**
   * 獲取系統(tǒng)相機
   * 
   * @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();
    };

  }
/**
** 實現(xiàn)界面回調處理
*/
  @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) {
        // 獲取當前相機參數(shù)
        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) {
              // 拍照 第三個參數(shù)為拍照回調
              mCamera.takePicture(null, null, new Camera.PictureCallback() {
                @Override
                public void onPictureTaken(byte[] data, Camera camera) {
                  // data為完整數(shù)據(jù)
                  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);
      }
    });
  }

   /* 根據(jù)需求的寬和高以及圖片實際的寬和高計算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;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論