android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例
實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能結(jié)果如下
第一步:
activity_takephoto.xml布局用SurfaceView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_takephoto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android_27.TakephotoActivity"> <SurfaceView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/sv_takephoto" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:id="@+id/b_OK" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="切換" android:id="@+id/b_qh" /> </LinearLayout> </RelativeLayout>
第二步:TakephotoActivity類
public class TakephotoActivity extends AppCompatActivity { private SurfaceView sv_takephoto; private Button b_OK; private Button b_qh; private Camera camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_takephoto); //獲得SurfaceView的ID sv_takephoto =(SurfaceView) findViewById(R.id.sv_takephoto); //獲得拍照的ID b_OK =(Button)findViewById(R.id.b_OK); //獲得切換鏡頭的ID b_qh =(Button)findViewById(R.id.b_qh); sv_takephoto.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { //打開相機(jī) camera = Camera.open(); //給相機(jī)設(shè)置參數(shù) Camera.Parameters parameters= camera.getParameters(); //設(shè)置保存的格式 parameters.setPictureFormat(PixelFormat.JPEG); parameters.set("jpeg-quality",85); camera.setParameters(parameters); //將畫面展示到SurfaceView中 try { camera.setPreviewDisplay(sv_takephoto.getHolder()); //開啟預(yù)覽 camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } }); //拍照的點(diǎn)擊事件 b_OK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //拍照 camera.takePicture(null, null, new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] bytes, Camera camera) { //將字節(jié)數(shù)組轉(zhuǎn)成圖片 Bitmap bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length); try { FileOutputStream fos=new FileOutputStream("/mnt/sdcard/DCIM/"+System.currentTimeMillis()+".jpg"); bitmap.compress(Bitmap.CompressFormat.PNG,85,fos); camera.stopPreview(); camera.startPreview(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }); } }); } }
第三步:在AndroidManifest.xml中加權(quán)限
<uses-permission android:name="android.permission.CAMERA" />
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件示例【附demo源碼下載】
- Android自定義相機(jī)實(shí)現(xiàn)定時(shí)拍照功能
- Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能
- Android仿微信選擇圖片和拍照功能
- Android實(shí)現(xiàn)手機(jī)拍照功能
- Android調(diào)用手機(jī)拍照功能的方法
- Android啟動(dòng)相機(jī)拍照并返回圖片
- Android仿微信發(fā)表說說實(shí)現(xiàn)拍照、多圖上傳功能
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android手機(jī)拍照或選取圖庫圖片作為頭像
- Android編程實(shí)現(xiàn)拍照功能的2種方法分析
相關(guān)文章
淺談Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能
這篇文章主要介紹了Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能,需要的朋友可以參考下2017-05-05RecyclerView實(shí)現(xiàn)插入和刪除
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)插入和刪除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android實(shí)現(xiàn)圖片在屏幕內(nèi)縮放和移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android控制圖片在屏幕內(nèi)縮放和移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Android實(shí)現(xiàn)文件上傳和下載倒計(jì)時(shí)功能的圓形進(jìn)度條
這篇文章主要介紹了Android實(shí)現(xiàn)文件上傳和下載倒計(jì)時(shí)功能的圓形進(jìn)度條,需要的朋友可以參考下2017-09-09使用RadioButton+Fragment實(shí)現(xiàn)底部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了使用RadioButton+Fragment實(shí)現(xiàn)底部導(dǎo)航欄效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06去掉activity默認(rèn)動(dòng)畫效果的簡單方法
下面小編就為大家?guī)硪黄サ鬭ctivity默認(rèn)動(dòng)畫效果的簡單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-03-03android引導(dǎo)用戶開啟自啟動(dòng)權(quán)限的方法
今天小編就為大家分享一篇android引導(dǎo)用戶開啟自啟動(dòng)權(quán)限的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07