欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

android7自定義相機(jī)預(yù)覽及拍照功能

本文實(shí)例為大家分享了Android實(shí)現(xiàn)攝像頭切換,拍照及保存到相冊,預(yù)覽等功能,解決android7拍照之后不能連續(xù)預(yù)覽的問題、參數(shù)設(shè)置相關(guān)問題以及前后攝像頭語言顛倒等問題。

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的集賢網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

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; 
 //啟動(dòng)攝像機(jī) 
 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); 
 
  // 預(yù)覽控件 
  surfaceView = (SurfaceView) this 
    .findViewById(R.id.surfaceView); 
  // 設(shè)置參數(shù) 
  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<String> lists = new ArrayList<>(); 
   for (int i = 0; i < permissions.length; i++) { 
    if (ActivityCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) { 
 
     } 
     lists.add(permissions[i]); 
    } 
   } 
   if (lists.size() > 0) { 
    String[] ps = new String[lists.size()]; 
    for (int i = 0; i < lists.size(); i++) { 
     ps[i] = lists.get(i); 
    } 
    ActivityCompat.requestPermissions(this, ps, 1); 
   } 
  } 
 } 
 
 @Override 
 protected void onRestart() { 
  super.onRestart(); 
 } 
 
 @Override 
 protected void onStart() { 
  super.onStart(); 
 } 
 
 private void takePhoto() { 
  if (!isopen_camara) { 
   previewCallBack = new SurfaceCallback(); 
   surfaceView.getHolder().addCallback(previewCallBack); 
  } else { 
   autoTakePhoto(); 
  } 
 } 
 
 
 @Override 
 protected void onResume() { 
  super.onResume(); 
  if(mCamera == null){ 
   if (!isopen_camara) { 
    previewCallBack = new SurfaceCallback(); 
    surfaceView.getHolder().addCallback(previewCallBack); 
   } 
  } 
 
 } 
 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
   case R.id.return_back: 
    finish(); 
    break; 
   case R.id.tvswitch_camera: 
    switchCamara(); 
    break; 
  } 
 } 
 
 
 // 預(yù)覽界面回調(diào) 
 private final class SurfaceCallback implements SurfaceHolder.Callback { 
  // 預(yù)覽界面被創(chuàng)建 
  public void surfaceCreated(SurfaceHolder holder) { 
   try { 
    //1代表打開后置攝像頭,0代表打開前置攝像頭. 
    mCamera = Camera.open(cameraPosition);// 打開攝像頭 
    setParams(holder,cameraPosition); 
   } catch (Exception e) { 
    e.printStackTrace(); 
    if (mCamera != null) { 
     mCamera.stopPreview(); 
     mCamera.lock(); 
     mCamera.release(); 
     mCamera = null; 
    } 
    finish(); 
    //Toast.makeText(getApplicationContext(), "該手機(jī)不支持自動(dòng)拍照功能", Toast.LENGTH_LONG).show(); 
   } 
  } 
 
  public void surfaceChanged(SurfaceHolder holder, int format, int width, 
         int height) { 
   System.out.println("surfaceChanged"); 
   isopen_camara = true; 
   //autoTakePhoto(); 
  } 
 
  // 預(yù)覽界面被銷毀 
  public void surfaceDestroyed(SurfaceHolder holder) { 
   System.out.println("surfaceDestroyed"); 
   if(!isopen_camara) 
    return; 
   if (mCamera != null) { 
    holder.removeCallback(this); 
    mCamera.setPreviewCallback(null); 
    mCamera.stopPreview(); 
    mCamera.lock(); 
    mCamera.release(); 
    mCamera = null; 
   } 
  } 
 
 } 
 
 public void reset(){ 
  Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
  if (mCamera != null) { 
   if(surfaceView != null && surfaceView.getHolder() != null && previewCallBack != null){ 
    surfaceView.getHolder().removeCallback(previewCallBack); 
   } 
   mCamera.setPreviewCallback(null); 
   mCamera.stopPreview(); 
   mCamera.lock(); 
   mCamera.release(); 
   mCamera = null; 
 
   mCamera = Camera.open(cameraPosition); 
   if (null != mholder) 
    setParams(mholder,cameraInfo.facing); 
  } 
 
 } 
 
 @Override 
 protected void onDestroy() { 
  super.onDestroy(); 
  if (mCamera != null) { 
   if(surfaceView != null && surfaceView.getHolder() != null && previewCallBack != null){ 
    surfaceView.getHolder().removeCallback(previewCallBack); 
   } 
   mCamera.setPreviewCallback(null); 
   mCamera.stopPreview(); 
   mCamera.lock(); 
   mCamera.release(); 
   mCamera = null; 
  } 
  ButterKnife.unbind(this); 
  EventBus.getDefault().unregister(this); 
 } 
 
 private void setParams(SurfaceHolder mySurfaceView,int postion) { 
  try { 
   int PreviewWidth = 0; 
   int PreviewHeight = 0; 
   WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);//獲取窗口的管理器 
   /*PreviewWidth = display.(); 
   PreviewHeight = display.getHeight();*/ 
 
   Camera.Parameters parameters = mCamera.getParameters(); 
   // 選擇合適的預(yù)覽尺寸 
   List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes(); 
 
   // 如果sizeList只有一個(gè)我們也沒有必要做什么了,因?yàn)榫退粋€(gè)別無選擇 
   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) {*/ 
     if (cur.width >= PreviewWidth 
       && cur.height >= PreviewHeight) { 
      PreviewWidth = cur.width; 
      PreviewHeight = cur.height; 
      break; 
     } 
 
    } 
   } 
   parameters.setPreviewSize(PreviewWidth, PreviewHeight); //獲得攝像區(qū)域的大小 
   //parameters.setPreviewFrameRate(3);//每秒3幀 每秒從攝像頭里面獲得3個(gè)畫面 
   //parameters.setPreviewFpsRange(3,); 
   List<int[]> list = parameters.getSupportedPreviewFpsRange(); 
   int[] v = null; 
   int index = 0; 
   int min = 0; 
   for (int i = 0; i < list.size(); i++) { 
    v = list.get(i); 
    if (v[0] > min) { 
     min = v[0]; 
     index = i; 
    } 
   } 
   parameters.setPreviewFpsRange(list.get(index)[0], list.get(index)[1]); 
   parameters.setPictureFormat(PixelFormat.JPEG);//設(shè)置照片輸出的格式 
   parameters.set("jpeg-quality", 85);//設(shè)置照片質(zhì)量 
   parameters.setPictureSize(PreviewWidth, PreviewHeight);//設(shè)置拍出來的屏幕大小 
   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);//把上面的設(shè)置 賦給攝像頭 
   mCamera.setPreviewDisplay(mySurfaceView);//把攝像頭獲得畫面顯示在SurfaceView控件里面 
   mholder = mySurfaceView; 
   mCamera.setPreviewCallback(new Camera.PreviewCallback() { 
    @Override 
    public void onPreviewFrame(byte[] data, Camera camera) { 
 
    } 
   }); 
   mCamera.startPreview();//開始預(yù)覽 
   // 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()); 
       } 
      }); 
     } 
    } 
   } 
  }); 
 } 
 
 // 照片回調(diào) 
 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();//得到攝像頭的個(gè)數(shù) 
 
  for (int i = 0; i < cameraCount; i++) { 
   Camera.getCameraInfo(i, cameraInfo);//得到每一個(gè)攝像頭的信息 
   if (cameraPosition == 1) { 
    //現(xiàn)在是后置,變更為前置 
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {//代表攝像頭的方位,CAMERA_FACING_FRONT前置  CAMERA_FACING_BACK后置 
     if(surfaceView != null && surfaceView.getHolder() != null && previewCallBack != null){ 
      surfaceView.getHolder().removeCallback(previewCallBack); 
     } 
     mCamera.setPreviewCallback(null); 
     mCamera.stopPreview();//停掉原來攝像頭的預(yù)覽 
     mCamera.lock(); 
     mCamera.release();//釋放資源 
     mCamera = null;//取消原來攝像頭 
     mCamera = Camera.open(i);//打開當(dāng)前選中的攝像頭 
     /* try { 
      if (null != mholder) 
       mCamera.setPreviewDisplay(mholder);//通過surfaceview顯示取景畫面 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     mCamera.startPreview();//開始預(yù)覽*/ 
     if (null != mholder) 
      setParams(mholder,Camera.CameraInfo.CAMERA_FACING_BACK); 
     cameraPosition = 0; 
     break; 
    } 
   } else { 
    //現(xiàn)在是前置, 變更為后置 
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//代表攝像頭的方位,CAMERA_FACING_FRONT前置  CAMERA_FACING_BACK后置 
     if(surfaceView != null && surfaceView.getHolder() != null && previewCallBack != null){ 
      surfaceView.getHolder().removeCallback(previewCallBack); 
     } 
     mCamera.setPreviewCallback(null); 
     mCamera.stopPreview();//停掉原來攝像頭的預(yù)覽 
     mCamera.lock(); 
     mCamera.release();//釋放資源 
     mCamera = null;//取消原來攝像頭 
     mCamera = Camera.open(i);//打開當(dāng)前選中的攝像頭 
     /*try { 
      if (null != mholder) 
       mCamera.setPreviewDisplay(mholder);//通過surfaceview顯示取景畫面 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     mCamera.startPreview();//開始預(yù)覽*/ 
     if (null != mholder) 
      setParams(mholder,Camera.CameraInfo.CAMERA_FACING_FRONT); 
     cameraPosition = 1; 
     break; 
    } 
   } 
 
  } 
 } 
 
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event) { 
  if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {///音量減拍照 
   takePhoto(); 
   return true; 
  } 
  return super.onKeyDown(keyCode, event); 
 
 } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:android7自定義相機(jī)預(yù)覽及拍照功能
網(wǎng)站地址:http://aaarwkj.com/article34/gjcgse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)頁設(shè)計(jì)公司網(wǎng)站營銷、營銷型網(wǎng)站建設(shè)、定制開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)
岛国av在线免费观看| 国产农村熟妇av国语对白| 日本免费一区二区三区等视频| 欧美日本国产专区一区| 在线观看中文字幕有码| 久久视频在线播放视频| 日本不卡视频二区三区| 国产精品中文字幕第一区| 国产精品久久电影观看| 精品人妻一区二区三区免费视频 | 欧美日韩国产激情高清| 国产麻豆精品二区视频| 免费看欧美黄片在线看| av在线免费观看美日韩| 免费久久人人爽人人爽| 国产一级av在线播放| av大全网站免费一区二区| 一区二区中文字幕日本韩国| 精品国产一区二区三级四区| 国产精品亚洲一区二区在| 精品国产亚洲av未满十八| av黄色资源在线观看| 日韩在线视频网站不卡| 国产超码片内射在线| 丰满人妻熟妇乱精品视频| 丝袜美腿一区二区三区| 说中文字幕的黄色大网站| 久久精品亚洲毛片美女极品| av在线免费观看大全| 亚洲日本av一区二区| 一级丰满少妇av大片| 欧美大片在线观看高清| 韩国三级福利在线观看| 亚洲久久精品一区二区| 99亚洲综合一区二区三区| 欧美特黄高清在线观看| 黄片在线免费在线播放| 很色很污无遮挡的网色污| 亚洲午夜一区二区三区精品| 少妇毛片一区二区三区| 日韩黄色一级片在线观看|