您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用Android拍照功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
直接上代碼
1、CameraDemoActivity.java(主界面)
package org.winplus.camera; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import android.app.Activity; import android.content.pm.ActivityInfo; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.text.format.DateFormat; import android.util.Log; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; public class CameraDemoActivity extends Activity{ private final static String TAG = "CameraActivity"; private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private Camera camera; private File picture; private Button btnSave; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); setupViews(); } private void setupViews(){ surfaceView = (SurfaceView) findViewById(R.id.camera_preview); // Camera interface to instantiate components surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); btnSave = (Button) findViewById(R.id.save_pic); btnSave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { takePic(); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_SEARCH) { takePic(); return true; } return super.onKeyDown(keyCode, event); } private void takePic() { camera.stopPreview();// stop the preview camera.takePicture(null, null, pictureCallback); // picture } // Photo call back Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { //@Override public void onPictureTaken(byte[] data, Camera camera) { new SavePictureTask().execute(data); camera.startPreview(); } }; // save pic class SavePictureTask extends AsyncTask<byte[], String, String> { @Override protected String doInBackground(byte[]... params) { String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg"; Log.i(TAG, "fname="+fname+";dir="+Environment.getExternalStorageDirectory()); //picture = new File(Environment.getExternalStorageDirectory(),fname);// create file picture = new File(Environment.getExternalStorageDirectory()+"/"+fname); try { FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream fos.write(params[0]); // Written to the file fos.close(); } catch (Exception e) { e.printStackTrace(); } return null; } } // SurfaceHodler Callback handle to open the camera, off camera and photo size changes SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() { public void surfaceCreated(SurfaceHolder holder) { Log.i(TAG, "surfaceCallback===="); camera = Camera.open(); // Turn on the camera try { camera.setPreviewDisplay(holder); // Set Preview } catch (IOException e) { camera.release();// release camera camera = null; } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.i(TAG,"====surfaceChanged"); Camera.Parameters parameters = camera.getParameters(); // Camera parameters to obtain parameters.setPictureFormat(PixelFormat.JPEG);// Setting Picture Format // parameters.set("rotation", 180); // Arbitrary rotation camera.setDisplayOrientation(0); // parameters.setPreviewSize(400, 300); // Set Photo Size camera.setParameters(parameters); // Setting camera parameters camera.startPreview(); // Start Preview } public void surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG,"====surfaceDestroyed"); camera.stopPreview();// stop preview camera.release(); // Release camera resources camera = null; } }; }
2、main.xml(布局文件)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.view.SurfaceView android:id="@+id/camera_preview" android:layout_width="800dip" android:layout_height="600dip" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:layout_gravity="center_vertical|center_horizontal" /> <Button android:id="@+id/save_pic" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/txt_save" /> </LinearLayout>
3、別忘了添加權限:
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用Android拍照功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。