您好,登錄后才能下訂單哦!
這篇文章主要介紹android如何實(shí)現(xiàn)簡單拍照功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
拍照可以調(diào)用系統(tǒng)的相機(jī)進(jìn)行拍照,拍完保存到相應(yīng)的SD卡目錄。最近要用到拍照這個(gè)功能,首先試了下調(diào)用相機(jī)拍照,保存到相應(yīng)目錄,然后用imageview顯示的時(shí)候感覺,很模糊。可能是圖片被壓縮的太狠了。所以自己使用Camera寫了一個(gè)拍照的功能,拍出來的還挺清晰的,不過文件有點(diǎn)大一張圖片差不多1.7M。如果想拍高清圖可以使用下面的代碼。
代碼很簡單,我就不做過多解釋了。
package com.example.testsimple; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.text.format.DateFormat; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private Camera camera; private File picture; private Button btnSave; private final static String TAG = "CameraActivity"; private ImageView open_picIcon; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* Intent intent=new Intent(MainActivity.this,MyCameraActivity.class); startActivity(intent); finish();*/ 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); open_picIcon=(ImageView) findViewById(R.id.open_picIcon); btnSave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { surfaceView.setVisibility(View.VISIBLE); open_picIcon.setVisibility(View.GONE); 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()+"/images/"+fname); File file= picture.getParentFile(); if(!file.exists()) file.mkdir(); 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; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); Bitmap bitmap=getDiskBitmap(picture.getPath()); surfaceView.setVisibility(View.GONE); open_picIcon.setVisibility(View.VISIBLE); open_picIcon.setImageBitmap(bitmap); } } private Bitmap getDiskBitmap(String pathString) { Bitmap bitmap = null; try { File file = new File(pathString); if(file.exists()) { bitmap = BitmapFactory.decodeFile(pathString); } } catch (Exception e) { // TODO: handle exception } return bitmap; } // 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; } }; }
拍完以后自動(dòng)保存到SD卡中的images目錄下
下面是圖片存放路徑
1.8M 確實(shí)有點(diǎn)大,不過是高清圖片
以上是“android如何實(shí)現(xiàn)簡單拍照功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。