调用系统相机,然后在自己的surfaceview上预览,拍照,不废话,直接上源码

 package com.example.customecamera;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View; public class MainActivity extends Activity implements SurfaceHolder.Callback { SurfaceView mSurfaceview;
Camera mCamera;
SurfaceHolder mHolder; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceview = (SurfaceView) findViewById(R.id.surfaceview);
mHolder = mSurfaceview.getHolder();
mHolder.addCallback(this);
} PictureCallback callback = new PictureCallback() { @Override
public void onPictureTaken(byte[] data, Camera camera) {
if(data != null) {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/" + "temp.jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data, 0, data.length);
Intent intent = new Intent(MainActivity.this , OtherActivity.class);
intent.putExtra("pic_path", file.getAbsolutePath());
startActivity(intent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }; @SuppressWarnings("deprecation")
public void caputer(View v) {
Parameters parameters = mCamera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPictureSize(400, 600);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.autoFocus(new AutoFocusCallback() { @Override
public void onAutoFocus(boolean success, Camera camera) {
mCamera.takePicture(null, null, callback);
}
}); } public Camera getCamera() {
Camera camera = Camera.open();
return camera;
} public void relaseCamer() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
} public void startPreview(Camera camera, SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Override
protected void onPause() {
super.onPause();
relaseCamer();
} @Override
protected void onResume() {
super.onResume();
if (mCamera == null) {
mCamera = getCamera();
if(mHolder != null) {
startPreview(mCamera, mHolder);
}
}
} @Override
public void surfaceCreated(SurfaceHolder holder) {
startPreview(mCamera, mHolder);
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mCamera.stopPreview();
startPreview(mCamera, mHolder);
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
relaseCamer();
} }
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="400dp" />
<Button
android:id="@+id/button1"
android:layout_below="@id/surfaceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="caputer"
android:text="拍照" /> </RelativeLayout>
 package com.example.customecamera;

 import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView; public class OtherActivity extends Activity {
ImageView iv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
iv = (ImageView) findViewById(R.id.imageView1);
String path = getIntent().getStringExtra("pic_path");
if(!TextUtils.isEmpty(path)) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
if(bitmap != null) {
iv.setImageBitmap(bitmap);
}
}
} }
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/> </LinearLayout>
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.customecamera"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity" >
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
</manifest>

android自定义拍照的更多相关文章

  1. Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)

    Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是 ...

  2. Android系统拍照源码

    个人对于Android系统拍照的一些总结:一种自定义图片拍照路径 ,另一种直接利用Android拍照后经过处理的缩略图 特别注意第一种方式需要增加SDK读写权限: <uses-permissio ...

  3. android 自定义相机

    老规矩,先上一下项目地址:GitHub:https://github.com/xiangzhihong/CameraDemo 方式: 调用Camera API 自定义相机 调用系统相机 由于需求不同, ...

  4. Android Camera2 拍照(四)——对焦模式

    原文:Android Camera2 拍照(四)--对焦模式 本篇将重点介绍使用Camera2 API进行手动对焦的设置,以及在手动对焦与自动对焦模式之间切换. 一.手动对焦响应事件 首先我们要实现点 ...

  5. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  6. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  7. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  8. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  9. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

随机推荐

  1. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  2. CSS笔记2

    1.     CSS基础选择器 html负责结构 ,css负责样式,js负责行为 css写在head标签里面,容器style标签 <style type="text/css" ...

  3. c#内部类的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. 经典.net面试题目

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. protected : 保 ...

  5. 制作IOS企业版App网页扫描二维码下载安装

    有时候我们需要在XX网站的主页上去扫描二维码下载,那么ios开发中如何做到这一点呢. 我给大家解答一下,这也是在最近工作中用到的部分,在网上了解了一些. 下面给大家分解一下步骤: 1.Plist 和 ...

  6. 小米网站登录源码C#版

    一步一步做,肯定能成功 HttpHelper类请从网络上搜索 string postData = getPostData(); HttpHelper ht = new HttpHelper(); Ht ...

  7. jQueryMobile引入文件后样式无法正常显示

    jQueryMobile引入文件后样式无法正常显示解决方法: jQuery文件必须放在jQueryMobile文件之前 eg:

  8. phpcms无刷新分页

    控制器添加一个函数: 添加一个静态页面ajax_message.html,在页面中添加如下代码: 在要分页的页面(我的是"show"页面)中添加如上图代码: phpcms无刷新分页 ...

  9. Unity透明材质Batch

    NO Batch  ? 游戏场景中存在大量例子的时候,DrallCall的压力很大,但是遍历一遍之后发现,为啥一样的粒子特效竟然没有合并,why?经过很多测试后发现,如果把透明材质的修改为非半透明的, ...

  10. 给Source Insight做个外挂系列之四--分析“Source Insight”

    外挂的目的就是将代码注入到其它进程中,所以必须要有目标进程才能完成注入,而所谓的目标进程通常是某软件的一部分或者是全部,所以要对目标程序有深入地了解.一般外挂都是针对某个应用程序开发的,其装载.运行都 ...