android第十九步拍照
知识点:清单文件android:screenOrientation="landscape"手机平板横向
<activity android:name="com.example.taskpicture.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>
requestWindowFeature(Window.FEATURE_NO_TITLE);不显示标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);设置全屏
Camera camera;照相机类
camera=Camera.open();打开摄像头
Camera.Parameters parameters=camera.getParameters();获取相机参数 ps:因为每个手机的相机参数不同最好看一下照相机的参数以免出错//Log.i("11", parameters.flatten());打印到日志中
parameters.setPreviewSize(1280, 720);浏览显示大小(在打印的参数中都可以看到)
parameters.setPreviewFrameRate(7);显示每秒几帧(在打印的参数中都可以看到)
parameters.setPictureSize(1280, 720);取景后的照片大小
parameters.setJpegQuality(80);jpg质量
camera.setParameters(parameters);
加载到Camera类中
camera.setPreviewDisplay(arg0);
设置图像SurfaceView中显示
camera.startPreview();开始浏览
camera.takePicture(null, null, new MyPictureCallback()); 参数为三个回调类,第一个是图像捕捉瞬间的回调类,第二个是原始(未压缩)图像数据的回调类,第三个是原始(压缩)图像数据的回调类
camera.autoFocus(null);对焦,参数为对焦的回调类
设计思路:当点击屏幕(重写onTouchEvent)是出现照相与对焦按钮然后继续拍照
布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <SurfaceView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/surfaceView"/> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" android:id="@+id/buttonlayout"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginRight="5dp" android:onClick="takepicture" android:text="@string/takepicture" android:id="@+id/takepicture"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/takepicture" android:layout_alignTop="@id/takepicture" android:layout_marginRight="20dp" android:onClick="takepicture" android:text="@string/autofouse" android:id="@+id/autofouse"/> </RelativeLayout> </FrameLayout>
public class MainActivity extends Activity { private View layout; private Camera camera; @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); layout = this.findViewById(R.id.buttonlayout); SurfaceView surfaceView=(SurfaceView)this.findViewById(R.id.surfaceView); surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceView.getHolder().setFixedSize(176, 144); surfaceView.getHolder().addCallback(new SurfaceCallback()); } public void takepicture(View v){ if(camera==null) return; switch(v.getId()){ case R.id.takepicture: camera.takePicture(null, null, new MyPictureCallback()); break; case R.id.autofouse: camera.autoFocus(null); break; } } private final class MyPictureCallback implements PictureCallback{ @Override public void onPictureTaken(byte[] arg0, Camera arg1) { //jpg图片数据 try { File jpgfile =new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); FileOutputStream outputStream = new FileOutputStream(jpgfile); outputStream.write(arg0); outputStream.close(); //camera.startPreview(); }catch(Exception e){ } } } private final class SurfaceCallback implements Callback{ @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder arg0) { try { camera=Camera.open();//打开摄像头 Camera.Parameters parameters=camera.getParameters();//获取相机参数 parameters.setPreviewSize(1280, 720);//浏览显示大小 parameters.setPreviewFrameRate(7);//显示每秒几帧 parameters.setPictureSize(1280, 720);//照片大小 parameters.setJpegQuality(80);//jpg质量 camera.setParameters(parameters); camera.setPreviewDisplay(arg0); //设置图像SurfaceView中显示 camera.startPreview(); }catch(Exception e){ } //Log.i("zhaoyilong", parameters.flatten()); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { if(camera!=null){ camera.release(); camera= null; } } } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ layout.setVisibility(ViewGroup.VISIBLE); return true; } return super.onTouchEvent(event); } @Override public 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; } }
权限
<uses-permission android:name="android.permission.CAMERA"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。