Camera使用时的一些相关问题处理
- 拍照旋转
- 显示模糊
- 设置参数
public class Camera1Activity extends AppCompatActivity implements SurfaceHolder.Callback {
private Button btn_camera;
private Camera mCamera;
private SurfaceView sv;
private SurfaceHolder sh;
private ImageView imgv_temp;
private Camera.PictureCallback pc = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// data为完整数据
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/CameraTest/pic/";
String fileName = "testCamera.png";
// 配置压缩的参数
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //获取当前图片的边界大小,而不是将整张图片载入在内存中,避免内存溢出
BitmapFactory.decodeByteArray(data,0,data.length, options);
options.inJustDecodeBounds = false;
options.inSampleSize = caculateSampleSize(options,sv.getWidth(),sv.getHeight());
Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length, options); // 解码文件
// Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length,options);
bitmap = rotaingImageView(90,bitmap);
saveBitmap(bitmap,filePath,fileName);
//刷新图库
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath,fileName))));
imgv_temp.setImageBitmap(bitmap);
imgv_temp.setVisibility(View.VISIBLE);
camera.stopPreview();
showBottom();
}
};
private BottomSheetDialog bsDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CameraView");
setContentView(R.layout.activity_camera_1);
showBottomSheetDialog();
imgv_temp = findViewById(R.id.imgv_temp);
sv = findViewById(R.id.sv);
// 初始化SurfaceHolder
sh = sv.getHolder();
sh.addCallback(this);
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
// 判断是否对焦成功
if (success) {
// 拍照 第三个参数为拍照回调
mCamera.takePicture(null, null, pc);
}
}
});
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
previewCamera();
}
private void previewCamera() {
// 在activity运行时绑定
if (mCamera == null) {
mCamera = getcCamera();
if (sh != null) {
showViews(mCamera, sh);
}
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// activity暂停时我们释放相机内存
clearCamera();
}
/**
* 计算出所需要压缩的大小
* @param options
* @param reqWidth 我们期望的图片的宽,单位px
* @param reqHeight 我们期望的图片的高,单位px
* @return
*/
private int caculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int sampleSize = 1;
int picWidth = options.outWidth;
int picHeight = options.outHeight;
if (picWidth > reqWidth || picHeight > reqHeight) {
int halfPicWidth = picWidth / 2;
int halfPicHeight = picHeight / 2;
while (halfPicWidth / sampleSize > reqWidth || halfPicHeight / sampleSize > reqHeight) {
sampleSize *= 2;
}
}
return sampleSize;
}
private void showBottom(){
bsDialog.show();
}
private void hindBottom(){
bsDialog.hide();
}
private void showBottomSheetDialog() {
if (bsDialog == null) {
bsDialog = new BottomSheetDialog(this);
//默认Cancelable和CanceledOnTouchOutside均为true
//bsDialog.setCancelable(true);
//bsDialog.setCanceledOnTouchOutside(true);
//为Dialog设置布局
bsDialog.setContentView(R.layout.dialog_bottom_sheet);
bsDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
imgv_temp.setVisibility(View.GONE);
mCamera.startPreview();
}
});
}
}
/**
* 获取系统相机
*
* @return
*/
private Camera getcCamera() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
camera = null;
}
return camera;
}
/**
* 与SurfaceView传播图像
*/
private void showViews(Camera camera, SurfaceHolder holder) {
// 预览相机,绑定
try {
int previewWidth = 0;
int previewHeight = 0;
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);//获取窗口的管理器
Display display = wm.getDefaultDisplay();//获得窗口里面的屏幕
Camera.Parameters params = mCamera.getParameters();
// 选择合适的预览尺寸
List<Camera.Size> sizeList = params.getSupportedPreviewSizes();
// 如果sizeList只有一个我们也没有必要做什么了,因为就他一个别无选择
if (sizeList.size() > 1) {
Iterator<Camera.Size> itor = sizeList.iterator();
while (itor.hasNext()) {
Camera.Size cur = itor.next();
if (cur.width >= previewWidth
&& cur.height >= previewHeight) {
previewWidth = cur.width;
previewHeight = cur.height;
break;
}
}
}
//选择合适的拍照尺寸
List<Camera.Size> sizeList2 = params.getSupportedPictureSizes();
int picWidth = 0;
int picHeight = 0;
if (previewWidth!=0 && previewHeight!=0){
//选择与预览尺寸长宽比最接近的一个size
double ratioPreview = previewWidth*1.00 / previewHeight;
double diff = 1000;
double curRatio;
double curDiff;
Camera.Size sizePic;
for (int i = 0; i < sizeList2.size(); i++) {
sizePic = sizeList2.get(i);
curRatio = sizePic.width*1.00 / sizePic.height;
curDiff = Math.abs(curRatio - ratioPreview);
if (curDiff < diff){
diff = curDiff;
picWidth = sizePic.width;
picHeight = sizePic.height;
if (curDiff <= 0.00){
break;
}
}
}
}else {
if (sizeList2.size() > 1) {
Iterator<Camera.Size> itor2 = sizeList2.iterator();
while (itor2.hasNext()) {
Camera.Size cur = itor2.next();
if (cur.width >= picWidth
&& cur.height >= picHeight) {
picWidth = cur.width;
picHeight = cur.height;
break;
}
}
}
}
params.setPreviewSize(previewWidth, previewHeight); //获得摄像区域的大小
params.setPreviewFrameRate(3);//每秒3帧 每秒从摄像头里面获得3个画面
params.setPictureFormat(PixelFormat.JPEG);//设置照片输出的格式
params.set("jpeg-quality", 85);//设置照片质量
params.setPictureSize(picWidth, picHeight);//设置拍出来的屏幕大小
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
// 设置对焦方式,这里设置自动对焦
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
try {
camera.setParameters(params);
}catch (Exception e){
Log.i("aa","e = "+e.toString());
}
camera.setPreviewDisplay(holder);
// 系统相机默认是横屏的,我们要旋转90°
camera.setDisplayOrientation(90);
// 开始预览
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 释放相机的内存
*/
private void clearCamera() {
// 释放hold资源
if (mCamera != null) {
// 停止预览
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
// 释放相机资源
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 开始预览
showViews(mCamera, sh);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// 重启功能
mCamera.stopPreview();
showViews(mCamera, sh);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 释放
clearCamera();
}
public int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
public Bitmap rotaingImageView(int angle , Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// 创建新的图片
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
private void saveBitmap(Bitmap bitmap,String filePath,String fileName){
// 2. 判断是否有缓存的文件夹
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
// 3. 存储图片到sdcard
File file = new File(dir,fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}