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.htm
public class MainActivity extends Activity { private SurfaceView sv; private SurfaceHolder holder; private Camera mCamera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(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.setFixedSize(3264,2448); 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) { // System.out.println("data"+data.length); File images=new File(Environment.getExternalStorageDirectory()+"//TextLocation"); FileOutputStream out=null; if(!images.exists()) images.mkdirs(); Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); // 格式化时间 String filename = format.format(date) + ".jpg"; File image=new File(images,filename); 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 { camera.startPreview(); } } } class MyCallback implements Callback { @Override public void surfaceCreated(SurfaceHolder holder) { //初始化相机 mCamera=Camera.open(); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (holder.getSurface() == null) { // preview surface does not exist return; } mCamera.stopPreview(); //可以将所有参数信息一行行显示,具体方法可以参见我的另外一篇博客: http://www.cnblogs.com/bokeofzp/p/4743108.html // System.out.println(mCamera.getParameters().flatten()); Camera.Parameters parms=mCamera.getParameters(); parms.setPictureFormat(ImageFormat.JPEG);//设置图片的格式 int w=sv.getWidth(); int h=sv.getHeight(); List<Size> sizes2=parms.getSupportedPreviewSizes(); for(Size s:sizes2) System.out.println(s.width+","+s.height); parms.setPreviewSize(1920,1088);//预览大小 parms.setPictureSize(3264,2448);//设置照片的大小 // Size size= parms.getPictureSize(); // Size size2= parms.getPreviewSize(); // System.out.println(size2.width); try { //一定要将属性值返回去,否则设置无效 mCamera.setParameters(parms); mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { mCamera.release(); mCamera=null; } } }