1、首先得加入权限,其中清单文件如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mycamera" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mycamera.MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2、main_xml文件,
android:layout_weight="0"是显示的权限,越大权限越小,这是线性布局里起效
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="com.example.mycamera.MainActivity$PlaceholderFragment" > <SurfaceView android:layout_weight="100" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/camera" /> <RelativeLayout android:layout_weight="0" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:layout_alignParentRight="true" android:id="@+id/pz" android:onClick="paizhao" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="对焦" android:id="@+id/dj" android:layout_toLeftOf="@+id/pz" android:onClick="duijiao" /> </RelativeLayout> </LinearLayout>
3、实现代码,其中观看相机硬件参数的结果时候是密密麻麻的为了方便观看结果可以看我的这篇博客:http://www.cnblogs.com/bokeofzp/p/4743108.html
public class MainActivity extends ActionBarActivity {//ActionBarActivity private SurfaceView sv; private SurfaceHolder holder; private Camera mCamera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏 setContentView(R.layout.activity_main); sv=(SurfaceView) findViewById(R.id.camera); holder=sv.getHolder(); holder.addCallback(new MyCallback()); } public void duijiao(View v) { //自动对焦 mCamera.autoFocus(null); } public void paizhao(View v) throws IOException { //拍照 mCamera.takePicture(null, null, new MyPictureCallback() ); } class MyPictureCallback implements PictureCallback { @Override public void onPictureTaken(byte[] data, Camera camera) { //dada就是拍照后压缩的数据 File image=new File(Environment.getDataDirectory(),"zp.jpg"); FileOutputStream out=null; try { out=new FileOutputStream(image); out.write(data); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "保存照片失败!", Toast.LENGTH_LONG).show(); } finally { mCamera.startPreview(); } } } class MyCallback implements Callback { @Override public void surfaceCreated(SurfaceHolder holder) { //初始化相机 mCamera=Camera.open(); MainActivity.this.holder=holder; //获取相机支持的所有参数 //获取相机支持的所有参数,这里获取的相机信息都打在一起,可以复制到word里面,点击crlt+h将所有的"; //"替换成换行符,点进”替换为“后,在替换界面点击“更多”,然后点击"特殊格式",选择“手动换行符”,这样 //可以将所有参数信息一行行显示,具体方法可以参见我的另外一篇博客:http://www.cnblogs.com/bokeofzp/p/4743108.html // System.out.println(mCamera.getParameters().flatten()); //得到相机参数后,根据相机支持的参数来设置自定义相机的参数 Camera.Parameters parms=mCamera.getParameters(); parms.setPictureFormat(ImageFormat.JPEG);//设置图片的格式 try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { mCamera.release(); mCamera=null; } } }
时间: 2024-11-09 10:32:22