首页 > 代码库 > android 拍照+拍照button 以及返回按钮布局
android 拍照+拍照button 以及返回按钮布局
公司最近做一个项目拍照总是崩溃,所以决定自己写一个拍照的方法,在网上调研一番,写了一个简单demo,现共享如下
主要CameraActivity
CameraActivity
package com.wondertek.video.camera; import java.io.FileOutputStream; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.Button; import android.widget.FrameLayout; import com.wondertek.xsgj.R; /** * * @Description:TODO 拍照优化 * @author:skyCc * @time:2014-6-6 下午5:03:10 */ public class CameraActivity extends Activity { public static final int CAMERA_RESULT = 1234; public static final int REQUEST_PICKER_ALBUM = 2345; private static String TAG = "CameraObserver"; public static String photopath = ""; Button buttonExit; Button buttonCapture; private Camera mCamera; private CameraViewNew mPreview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // CameraObserver已经将photopath路径传过来了 Log.d(TAG, "CameraActivity onCreate photopath = " + photopath); getWindow().clearFlags( WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.capture_activity); mCamera = getCameraInstance(); // 创建预览类,并与Camera关联,最后添加到界面布局中 mPreview = new CameraViewNew(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); preview.addView(mPreview); // requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置横屏模式以及全屏模式 // setContentView(new CameraView(this,photopath)); //设置View buttonCapture = (Button) findViewById(R.id.button_capture); buttonCapture.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { // TODO Auto-generated method stub mCamera.takePicture(null, null, myPicture); } }); } }); buttonExit=(Button) findViewById(R.id.button_exit); buttonExit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub finish(); } }); } /** 打开一个Camera */ public static Camera getCameraInstance() { Camera c = null; try { c = Camera.open(); } catch (Exception e) { Log.d(TAG, "打开Camera失败失败"); } return c; } private PictureCallback myPicture = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // TODO Auto-generated method stub Log.d(TAG, " onPictureTaken = " + photopath); try { if (photopath.length() > 0) data2file(data, photopath); } catch (Exception e) { } camera.stopPreview(); } private void data2file(byte[] w, String fileName) throws Exception {// 将二进制数据转换为文件的函数 FileOutputStream out = null; try { out = new FileOutputStream(fileName); out.write(w); out.close(); } catch (Exception e) { if (out != null) out.close(); throw e; } } }; }
CameraViewNew
package com.wondertek.video.camera; import java.io.IOException; import org.jivesoftware.smack.sasl.SASLMechanism.Success; import android.content.Context; import android.hardware.Camera; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; /** * @Description:TODO重新布局拍照的界面 * @author:skyCc * @time:2014-6-5 下午11:33:11 */ public class CameraViewNew extends SurfaceView implements Callback{ private final static String TAG="CameraView"; private Camera mCamera; private SurfaceHolder mHolder; public CameraViewNew(Context context,Camera camera) { super(context); mCamera=camera; mHolder=getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // TODO Auto-generated constructor stub } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub if(mHolder.getSurface()==null){ return ; } mCamera.stopPreview(); try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub mCamera.setPreviewCallback(null); mCamera.stopPreview(); mCamera.release(); mCamera =null; } }
capture_activity.xm
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:id="@+id/camera_preview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <Button android:id="@+id/button_capture" android:text="拍照" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <Button android:text="返回" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button_exit" android:layout_gravity="center" /> </LinearLayout>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。