热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

android7自定义相机预览及拍照功能

这篇文章主要为大家详细介绍了android7自定义相机预览及拍照功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现摄像头切换,拍照及保存到相册,预览等功能,解决android7拍照之后不能连续预览的问题、参数设置相关问题以及前后摄像头语言颠倒等问题。

import android.Manifest; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.graphics.PixelFormat; 
import android.hardware.Camera; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.Handler; 
import android.support.v4.app.ActivityCompat; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.TextView; 
 
import com.hama.tracker.R; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
 
 
public class CamaraActivity extends Activity implements View.OnClickListener { 
 
 @Bind(R.id.return_back) 
 TextView returnBack; 
 @Bind(R.id.tvswitch_camera) 
 TextView tvswitchCamera; 
 //启动摄像机 
 private Camera mCamera; 
 
 public boolean isopen_camara = false; 
 private SurfaceView surfaceView; 
 private SurfaceHolder mholder = null; 
 private SurfaceCallback previewCallBack; 
 private boolean isTakingPhoto;//是否正在拍照 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  verifyPermission(new String[]{Manifest.permission.CAMERA}); 
  // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  setContentView(R.layout.activity_camara); 
  ButterKnife.bind(this); 
 
  // 预览控件 
  surfaceView = (SurfaceView) this 
    .findViewById(R.id.surfaceView); 
  // 设置参数 
  surfaceView.getHolder().setKeepScreenOn(true); 
  surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
 
  returnBack.setOnClickListener(this); 
  tvswitchCamera.setOnClickListener(this); 
  EventBus.getDefault().register(this); 
 } 
 
 public void verifyPermission(String[] permissions) { 
  if (permissions != null) { 
   List lists = new ArrayList<>(); 
   for (int i = 0; i  0) { 
    String[] ps = new String[lists.size()]; 
    for (int i = 0; i  sizeList = parameters.getSupportedPreviewSizes(); 
 
   // 如果sizeList只有一个我们也没有必要做什么了,因为就他一个别无选择 
   if (sizeList.size() > 1) { 
    Iterator itor = sizeList.iterator(); 
    while (itor.hasNext()) { 
     Camera.Size cur = itor.next(); 
     /*if (cur.width >= PreviewWidth 
       && cur.height >= PreviewHeight) {*/ 
     if (cur.width >= PreviewWidth 
       && cur.height >= PreviewHeight) { 
      PreviewWidth = cur.width; 
      PreviewHeight = cur.height; 
      break; 
     } 
 
    } 
   } 
   parameters.setPreviewSize(PreviewWidth, PreviewHeight); //获得摄像区域的大小 
   //parameters.setPreviewFrameRate(3);//每秒3帧 每秒从摄像头里面获得3个画面 
   //parameters.setPreviewFpsRange(3,); 
   List list = parameters.getSupportedPreviewFpsRange(); 
   int[] v = null; 
   int index = 0; 
   int min = 0; 
   for (int i = 0; i  min) { 
     min = v[0]; 
     index = i; 
    } 
   } 
   parameters.setPreviewFpsRange(list.get(index)[0], list.get(index)[1]); 
   parameters.setPictureFormat(PixelFormat.JPEG);//设置照片输出的格式 
   parameters.set("jpeg-quality", 85);//设置照片质量 
   parameters.setPictureSize(PreviewWidth, PreviewHeight);//设置拍出来的屏幕大小 
   parameters.setRotation(180); //Java部分 
   if (Build.VERSION.SDK_INT >= 24) { 
    if(postion == 0){ 
     mCamera.setDisplayOrientation(90); 
    }else { 
     mCamera.setDisplayOrientation(270); 
    } 
   } else { 
    mCamera.setDisplayOrientation(90); 
   } 
 
 
 
   mCamera.setParameters(parameters);//把上面的设置 赋给摄像头 
   mCamera.setPreviewDisplay(mySurfaceView);//把摄像头获得画面显示在SurfaceView控件里面 
   mholder = mySurfaceView; 
   mCamera.setPreviewCallback(new Camera.PreviewCallback() { 
    @Override 
    public void onPreviewFrame(byte[] data, Camera camera) { 
 
    } 
   }); 
   mCamera.startPreview();//开始预览 
   // mPreviewRunning = true; 
  } catch (IOException e) { 
   Log.e("tag", e.toString()); 
  } 
 } 
 
 Handler handler = null; 
 
 private void autoTakePhoto() { 
  // 拍照前需要对焦 获取清析的图片 
  if (null == mCamera) return; 
  mCamera.autoFocus(new Camera.AutoFocusCallback() { 
   @Override 
   public void onAutoFocus(boolean success, Camera camera) { 
 
    if (success && isopen_camara) { 
     // 对焦成功 
     // Toast.makeText(MainActivity.this, "对焦成功 !!",Toast.LENGTH_SHORT).show(); 
     if(!isTakingPhoto) { 
      isTakingPhoto = true; 
      handler = new Handler(); 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        mCamera.takePicture(null, null, new MyPictureCallback()); 
       } 
      }); 
     } 
    } 
   } 
  }); 
 } 
 
 // 照片回调 
 private final class MyPictureCallback implements Camera.PictureCallback { 
  // 照片生成后 
  @Override 
  public void onPictureTaken(byte[] data, Camera camera) { 
   try { 
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
    Matrix matrix = new Matrix(); 
    matrix.setRotate(270); 
 
    File jpgFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/camera"); 
    if (!jpgFile.exists()) { 
     jpgFile.mkdir(); 
    } 
    File jpgFile1 = new File(jpgFile.getAbsoluteFile(), System.currentTimeMillis() + ".jpg"); 
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    FileOutputStream fos = new FileOutputStream(jpgFile1); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos); 
    ToastUtils.show(getApplicationContext(), getString(R.string.save_success)); 
    fos.close(); 
    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    Uri uri = Uri.fromFile(jpgFile1); 
    intent.setData(uri); 
    sendBroadcast(intent); 
   } catch (Exception e) { 
    e.printStackTrace(); 
   }finally { 
    if(Build.VERSION.SDK_INT>=24){ 
     reset(); 
    } 
    isTakingPhoto = false; 
   } 
  } 
 } 
 
 private int cameraPosition = 1;//0代表前置摄像头,1代表后置摄像头 
 
 private void switchCamara() { 
  //切换前后摄像头 
  int cameraCount = 0; 
  Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
  cameraCount = Camera.getNumberOfCameras();//得到摄像头的个数 
 
  for (int i = 0; i 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
孩子气zyj2
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有