您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Android中怎么實現拍照功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
解析:
1)判斷是否有攝像頭checkCameraHardware(this)
2)獲得相機camera = Camera.open(0);
3)把相機添加到mPreView = new SurfacePreView(this, mCamera);
4)實現拍照 mCamera.autoFocus
5)在拍照后使用mCamera.takePicture(null, null, mPicture);方法把圖片保存到sdcard
代碼實現:
/** * 使用照相機拍照 * @author chen.lin * */ public class CameraActivity extends Activity { private Camera mCamera;//相機 private SurfacePreView mPreView;//視圖 private FrameLayout mFrameLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_takephoto); mFrameLayout = (FrameLayout) findViewById(R.id.framelayout); if (checkCameraHardware(this)) { mCamera = getCamera(); mPreView = new SurfacePreView(this, mCamera); mFrameLayout.addView(mPreView); } } /** * 拍照對焦 * @param view */ public void takephoto(View view){ mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { mCamera.takePicture(null, null, mPicture); } }); } /** * 拍照后的圖片保存 */ private PictureCallback mPicture = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { try { File file = new File("/sdcard/file/aa.jpg"); if (!file.exists()) { file.mkdirs(); } FileOutputStream fos = new FileOutputStream(file); fos.write(data); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; /** * 判斷照相機是否存在 * @param context * @return */ private boolean checkCameraHardware(Context context){ if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { return true; } return false; } /** * 初始化照相機 * @return */ public static Camera getCamera(){ Camera camera = null; try { camera = Camera.open(0); } catch (Exception e) { e.printStackTrace(); } return camera; } /** * 實現拍照視圖 */ @SuppressWarnings("deprecation") private class SurfacePreView extends SurfaceView implements SurfaceHolder.Callback{ private Camera mCamera; private SurfaceHolder mHolder; public SurfacePreView(Context context, Camera camera) { super(context); mCamera = camera; mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder holder) { try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (holder.getSurface() == null) { return; } try { mCamera.stopPreview(); mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // mCamera = null; } } }
布局:
<?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" > <FrameLayout android:id="@+id/framelayout" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="takephoto" android:text="拍照" /> </LinearLayout>
上述就是小編為大家分享的Android中怎么實現拍照功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。