您好,登錄后才能下訂單哦!
Android中如何使用照相機(jī),相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
Android 照相機(jī)的實(shí)例應(yīng)用
SurfaceHolder.Callback
public class MyCameraDemo extends Activity { private SurfaceView surface = null ; private Button but = null ; private SurfaceHolder holder = null ; private Camera cam = null ; private boolean previewRunning = true ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.but = (Button) super.findViewById(R.id.but) ; this.surface = (SurfaceView) super.findViewById(R.id.surface) ; this.holder = this.surface.getHolder() ; this.holder.addCallback(new MySurfaceViewCallback()) ; //設(shè)置緩沖類型 this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) ; //設(shè)置分辨率 this.holder.setFixedSize(600, 350); this.but.setOnClickListener(new OnClickListenerImpl()) ; } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { if(MyCameraDemo.this.cam != null) { //自動對焦 MyCameraDemo.this.cam.autoFocus(new AutoFocusCallbackImpl()) ; } } } private class MySurfaceViewCallback implements SurfaceHolder.Callback { //當(dāng)預(yù)覽界面格式大小改變時,調(diào)用 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } //初次實(shí)例化界面調(diào)用 public void surfaceCreated(SurfaceHolder holder) { MyCameraDemo.this.cam = Camera.open(0) ; // 取得第一個攝像頭 //窗口服務(wù) WindowManager manager = (WindowManager) MyCameraDemo.this .getSystemService(Context.WINDOW_SERVICE); //取得display顯示對象 Display display = manager.getDefaultDisplay() ; //照相機(jī)參數(shù) Parameters param = MyCameraDemo.this.cam.getParameters() ; //將照相機(jī)預(yù)覽大小設(shè)置為display大小 param.setPreviewSize(display.getWidth(), display.getHeight()) ; param.setPreviewFrameRate(5) ; // 一秒5幀 param.setPictureFormat(PixelFormat.JPEG) ; // 圖片形式 param.set("jpen-quality", 80) ;//圖片質(zhì)量,最高100 MyCameraDemo.this.cam.setParameters(param) ; try { MyCameraDemo.this.cam.setPreviewDisplay(MyCameraDemo.this.holder) ; } catch (IOException e) { } MyCameraDemo.this.cam.startPreview() ; // 進(jìn)行預(yù)覽 MyCameraDemo.this.previewRunning = true ; // 已經(jīng)開始預(yù)覽 } @Override public void surfaceDestroyed(SurfaceHolder holder) { if(MyCameraDemo.this.cam != null) { if(MyCameraDemo.this.previewRunning) { MyCameraDemo.this.cam.stopPreview() ; // 停止預(yù)覽 MyCameraDemo.this.previewRunning = false ; } MyCameraDemo.this.cam.release() ; } } } private class AutoFocusCallbackImpl implements AutoFocusCallback { @Override public void onAutoFocus(boolean success, Camera camera) { if(success) { // 成功 MyCameraDemo.this.cam.takePicture(sc, pc, jpgcall) ; } } } private PictureCallback jpgcall = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // 保存圖片的操作 Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length); String fileName = Environment.getExternalStorageDirectory() .toString() + File.separator + "mldnphoto" + File.separator + "MLDN_" + System.currentTimeMillis() + ".jpg"; File file = new File(fileName) ; if (!file.getParentFile().exists()) { file.getParentFile().mkdirs() ; // 創(chuàng)建文件夾 } try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)) ; bmp.compress(Bitmap.CompressFormat.JPEG, 80, bos) ; // 向緩沖區(qū)之中壓縮圖片 bos.flush() ; bos.close() ; Toast.makeText(MyCameraDemo.this, "拍照成功,照片已保存在" + fileName + "文件之中!", Toast.LENGTH_SHORT) .show(); } catch (Exception e) { Toast.makeText(MyCameraDemo.this, "拍照失??!", Toast.LENGTH_SHORT) .show(); } MyCameraDemo.this.cam.stopPreview() ; MyCameraDemo.this.cam.startPreview() ; } } ; private ShutterCallback sc = new ShutterCallback(){ @Override public void onShutter() { // 按下快門之后進(jìn)行的操作 } } ; private PictureCallback pc = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { } } ; }
主布局函數(shù)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/but" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="照相" /> <SurfaceView android:id="@+id/surface" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
看完上述內(nèi)容,你們掌握Android中如何使用照相機(jī)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。