編輯:關於Android編程
結合精簡並優化了一下常用的拍照方法,實現了無預覽拍照,下面是一個工具類
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; } }
布局文件看一下
android:id="@+id/new_build_resize_layout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent" >
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" >
android:layout_height="50dp">
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
android:layout_height="fill_parent"
android:background="#ffffff"/>
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" >
android:layout_height="wrap_content" >
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/show_pwd" />
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" >
handler是什麼? handler是android給我們提供用來更新UI的一套機制,也是一套消息處理的機制,我們可以發送消息,也可以通過他處理消息。 為什麼要用han
之前用Linux Deploy 部署了Kali Linux 。讓我這陣子拿到平板有一半的時間是在用終端模擬器(Terminal Emulator)連接。安卓的終端模擬器,
微信聊天現在非常火,是因其界面漂亮嗎,哈哈,也許吧。微信每條消息都帶有一個氣泡,非常迷人,看起來感覺實現起來非常難,其實並不難。下面小編給大家分享實現代碼。先給大家展示下
Android基於Linux2.6+內核,我們看一張圖,以對Android系統的架構有個感性的認識。 我們從Kernel層簡單說明: 1、