时间:2021-05-20
大家好,这是一个简单的拍照功能,很简单的界面,一个显示图像区域SurfaceView一个“拍照”按钮。直接上代码!
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(布局文件)
3、别忘了添加权限:
源码下载:Android实现拍照功能
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍了Android编辑头像功能的简单实例,可以实现拍照,图片选取,裁剪。拍照publicstaticvoidstartCamera(Fragmentfra
本文实例讲述了Android编程实现拍照功能的2种方法。分享给大家供大家参考,具体如下:Android系统的照相功能,已实现2种方法,可供大家参考:1.调用系统
本文实例为大家分享了Android实现摄像头切换,拍照及保存到相册,预览等功能,解决android7拍照之后不能连续预览的问题、参数设置相关问题以及前后摄像头语
这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能。首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:activ
应用场景:在Android开发过程中,有时需要调用手机自身设备的功能,上篇文章主要侧重摄像头拍照功能的调用。本篇文章将综合实现拍照与视频的操作。知识点介绍:该部