您好,登錄后才能下訂單哦!
實現(xiàn)手機(jī)App實現(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) { } }); //拍照的點擊事件 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。