前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android实现拍照功能

Android实现拍照功能

作者头像
砸漏
发布2020-11-05 10:13:20
9350
发布2020-11-05 10:13:20
举报
文章被收录于专栏:恩蓝脚本

大家好,这是一个简单的拍照功能,很简单的界面,一个显示图像区域SurfaceView一个“拍照”按钮。直接上代码!

1、CameraDemoActivity.java(主界面)

代码语言:javascript
复制
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(布局文件)

代码语言:javascript
复制
<?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、别忘了添加权限:

代码语言:javascript
复制
<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实现拍照功能

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档