Android实现手机拍照功能
短信预约 -IT技能 免费直播动态提醒
本文实例为大家讲解如何轻松实现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软件编程有所帮助。
您可能感兴趣的文章:Android编程调用系统自带的拍照功能并返回JPG文件示例【附demo源码下载】android实现手机App实现拍照功能示例Android自定义相机实现定时拍照功能Android使用系统自带的相机实现一键拍照功能Android仿微信选择图片和拍照功能Android调用手机拍照功能的方法Android启动相机拍照并返回图片Android仿微信发表说说实现拍照、多图上传功能Android实现从本地图库/相机拍照后裁剪图片并设置头像Android手机拍照或选取图库图片作为头像Android编程实现拍照功能的2种方法分析
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341