編輯:關於Android編程
本文實例為大家講解如何輕松實現Android手機拍照功能,分享給大家供大家參考。具體如下:
一、布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SurfaceView android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <RelativeLayout android:id="@+id/buttonlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" > <Button android:id="@+id/takepicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:onClick="takepicture" android:text="@string/takepicture" /> <Button android:id="@+id/autofocus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/takepicture" android:layout_marginRight="20dp" android:layout_toLeftOf="@id/takepicture" android:onClick="takepicture" android:text="@string/autofocus" /> </RelativeLayout> </FrameLayout>
二、MainActivity.java
import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { private View layout; private Camera camera; @Override public 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.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().setKeepScreenOn(true); surfaceView.getHolder().addCallback(new SurfaceCallback()); } public void takepicture(View v) { if (camera != null) { switch (v.getId()) { case R.id.takepicture: camera.takePicture(null, null, new MyPictureCallback()); break; case R.id.autofocus: camera.autoFocus(null); break; } } } private final class MyPictureCallback implements PictureCallback { public void onPictureTaken(byte[] data, Camera camera) { try { File jpgFile = new File( Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); FileOutputStream outStream = new FileOutputStream(jpgFile); outStream.write(data); outStream.close(); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } private final class SurfaceCallback implements Callback { public void surfaceCreated(SurfaceHolder holder) { try { camera = Camera.open();// 打開攝像頭 Camera.Parameters parameters = camera.getParameters(); // Log.i("MainActivity", parameters.flatten()); parameters.setPreviewSize(800, 480); parameters.setPreviewFrameRate(5); parameters.setPictureSize(1024, 768); parameters.setJpegQuality(80); camera.setParameters(parameters); camera.setPreviewDisplay(holder); camera.startPreview();// 開始預覽 } catch (Exception e) { e.printStackTrace(); } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceDestroyed(SurfaceHolder holder) { 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); } }
三、添加權限
<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" />
效果如下:
希望本文所述對大家學習Android軟件編程有所幫助。
周末閒著沒事,寫了個手勢解鎖的view,實現起來也蠻快的,半天多一點時間就完事。把源碼和資源貼出來,給大家分享,希望對大家有用。效果,就跟手機上的九點手勢解鎖一樣,上個圖
把github上的PagerSlidingTabStrip稍作修改: tab的文字顏色選中變色(原版文字不變色) 栗子:http://download.csdn.ne
一、前言自從去年中微信添加搶紅包的功能,微信的電商之旅算是正式開始正式火爆起來。但是作為Android開發者來說,我們在搶紅包的同時意識到了很多問題,就是手動去搶紅包的速
本文均屬自己閱讀源碼的點滴總結,轉賬請注明出處謝謝。 歡迎和大家交流。qq:1037701636 email:[email protected] Androi