手机拍照功能的简单实现
生活随笔
收集整理的這篇文章主要介紹了
手机拍照功能的简单实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、main.xml
?
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><SurfaceView android:id="@+id/surfaceview"android:layout_width="fill_parent"android:layout_height="fill_parent"/><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:orientation="horizontal"><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="對焦"android:onClick="focus"android:layout_marginRight="10dp"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="拍照"android:onClick="takepicture"android:layout_marginRight="10dp"/></LinearLayout> </RelativeLayout>?
2、MainActivity
?
package com.njupt.takepicture1;import java.io.File; import java.io.FileOutputStream;import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager;public class MainActivity extends Activity {private SurfaceView surfaceview;private Camera camera;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);surfaceview = (SurfaceView) findViewById(R.id.surfaceview);SurfaceHolder holder = surfaceview.getHolder();holder.setFixedSize(176, 144);holder.setKeepScreenOn(true);holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);holder.addCallback(new MySurfaceCallback());}private class MySurfaceCallback implements Callback {@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {// TODO Auto-generated method stub}@Overridepublic void surfaceCreated(SurfaceHolder holder) {try {camera = Camera.open();Parameters params = camera.getParameters();params.setJpegQuality(90);params.setPictureSize(1024, 768);params.setPreviewFrameRate(10);camera.setParameters(params);camera.setPreviewDisplay(surfaceview.getHolder());camera.startPreview();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (camera != null) {camera.release();camera = null;}}}public void takepicture(View v) {camera.takePicture(null, null, new MyPictureCallback());}private class MyPictureCallback implements PictureCallback {@Overridepublic void onPictureTaken(byte[] data, Camera camera) {try {File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");FileOutputStream fos = new FileOutputStream(file);fos.write(data);camera.startPreview();} catch (Exception e) {e.printStackTrace();}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}?
3、AndroidManifest.xml
注冊權(quán)限
?
<uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>?
橫屏顯示:
?
<activityandroid:name="com.njupt.takepicture1.MainActivity"android:label="@string/app_name" android:screenOrientation="landscape">?
?
總結(jié)
以上是生活随笔為你收集整理的手机拍照功能的简单实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Adapter模式
- 下一篇: adb链接手机调试android应用