android调用摄像头偷拍demo 无声 无预览 一件拍照存储
结合精简并优化了一下常用的拍照方法,实现了无预览拍照,下面是一个工具类
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.Config; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.media.AudioManager; import android.os.Build; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; import com.cn.zongyi.safebox.Constants; import com.cn.zongyi.safebox.MyApplication; import com.cn.zongyi.safebox.ui.NewBuildSafeBoxActivity; import com.cn.zongyi.safebox.ui.R; public class CameraUtil { private SurfaceView sView; //画布视图 private SurfaceHolder surfaceHolder; //画布Holder public Camera camera; // 定义系统所用的照相机 private boolean isPreview = false;// 是否在浏览中 private AudioManager manager; //声音管理 private int volumn; //声音值 private String picPath = ""; private NewBuildSafeBoxActivity context; public CameraUtil(Context context) { this.context = (NewBuildSafeBoxActivity) context; } @SuppressWarnings("deprecation") public void initCameraFirst() { manager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); manager.setStreamMute(AudioManager.STREAM_SYSTEM, false); volumn = manager.getStreamVolume(AudioManager.STREAM_SYSTEM); if (volumn != 0) { // 如果需要静音并且当前未静音(muteMode的设置可以放在Preference中) manager.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); } sView = (SurfaceView) ((Activity) context).findViewById(R.id.sView); if (MyApplication.invadeMonitor()) { // 获得SurfaceView的SurfaceHolder surfaceHolder = sView.getHolder(); // 为surfaceHolder添加一个回调监听器 surfaceHolder.addCallback(new Callback() { public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceCreated(SurfaceHolder holder) { // surface被创建时打开摄像头 initCamera(); } // surface摧毁时释放摄像头 public void surfaceDestroyed(SurfaceHolder holder) { // 如果camera不为null ,释放摄像头 if (camera != null) { // 7.结束程序时,调用Camera的StopPriview()结束取景预览,并调用release()方法释放资源. if (isPreview) camera.stopPreview(); camera.release(); camera = null; } } }); // 设置该SurfaceView自己不维护缓冲 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } } @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") private void initCamera() { if (!isPreview) { int cameraCount = 0; Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); cameraCount = Camera.getNumberOfCameras(); // get cameras number if (cameraCount == 1) { // camera = Camera.open();// 调用Camera的open()方法打开相机。 Log.e("TAG", "无前置摄像头"); } else { Log.e("TAG", "__________4"); for (int camIdx = 0; camIdx < cameraCount; camIdx++) { Camera.getCameraInfo(camIdx, cameraInfo); // get camerainfo if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { // 代表摄像头的方位,目前有定义值两个分别为CAMERA_FACING_FRONT前置和CAMERA_FACING_BACK后置 try { camera = Camera.open(camIdx);// 调用Camera的open()方法打开相机。 } catch (RuntimeException e) { e.printStackTrace(); } } } } } if (camera != null && !isPreview) { try { camera.setPreviewDisplay(surfaceHolder); } catch (IOException e) { e.printStackTrace(); } // 开始预览 camera.startPreview(); isPreview = true; } } public PictureCallback myjpegCallback = new PictureCallback() { @SuppressLint("SimpleDateFormat") public void onPictureTaken(byte[] data, Camera camera) { Log.e("TAG", "拍照成功"); // 重置声音 manager.setStreamVolume(AudioManager.STREAM_SYSTEM, volumn, AudioManager.FLAG_ALLOW_RINGER_MODES); // 根据拍照所得的数据创建位图 final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); if (ExistSDCard()) { picPath = Constants.HEAD_PIC_PATH + new SimpleDateFormat("yyyyMMdd_HHmmss") .format(new Date()) + ".jpg"; File file = new File(picPath); FileOutputStream outStream = null; try { // 打开指定文件对应的输出流 outStream = new FileOutputStream(file); // 把位图输出到指定文件中 Matrix matrix = new Matrix(); Bitmap bm = Bitmap.createBitmap(1200, 1200 * bitmap.getHeight() / bitmap.getWidth(), Config.ARGB_8888);// 固定所拍相片大小 matrix.setScale( (float) bm.getWidth() / (float) bitmap.getWidth(), (float) bm.getHeight() / (float) bitmap.getHeight());// 注意参数一定是float哦 Canvas canvas = new Canvas(bm);// 用bm创建一个画布 // 可以往bm中画入东西了 canvas.drawBitmap(bitmap, matrix, null); bm.compress(CompressFormat.JPEG, 40, outStream); outStream.close(); } catch (IOException e) { e.printStackTrace(); } } else { Log.e("TAG", "SD卡不可用"); } camera.stopPreview(); camera.startPreview(); isPreview = true; MyApplication.getInstance().setUser_pic_path(picPath); context.cameraRefresh(picPath); } }; public static boolean ExistSDCard() { if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { return true; } else return false; } }
布局文件看一下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/new_build_resize_layout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:ignore="UselessParent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp">
<SurfaceView
android:id="@+id/sView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"/>
</RelativeLayout>
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/please_enter_a_password"
android:inputType="number"
android:maxLength="@integer/edit_length"
android:password="true"
tools:ignore="Deprecated" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/ckb_show_pwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/show_pwd" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/next_step_linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" >
<Button
android:id="@+id/next_step"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_build_safe_box_new_step_button" />
</LinearLayout>
</RelativeLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。