您的位置:首页 > 移动开发 > Android开发

android(43)(调用系统照相机功能)

2015-12-08 10:18 183 查看
1.调用系统的照相机功能:
布局文件:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:onClick="click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
/>
</LinearLayout>
业务逻辑代码:
public class MainActivity extends Activity {
private ImageView iv;
private File file ;//拍照存放的路径

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}

public void click(View view) {
Intent intent = new Intent();
// 指定拍照的意图。
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
file =  new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); // 指定保存文件的路径
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==100){
iv.setImageURI(Uri.fromFile(file));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
2.偷拍神器:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
android:onClick="click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照" />

<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="100dip"
android:layout_height="100dip"
/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
/>
</LinearLayout>
2.业务逻辑:
预览:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);//设置预览显示
mCamera.startPreview();//开启预览显示
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {

}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
}

try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
主界面:
public class MainActivity extends Activity {
private ImageView iv;
private Camera mCamera;
private CameraPreview mPreview;//照相机的一个预览

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
public void click(View view){
mCamera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
mCamera.takePicture(null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
File file = new File(Environment.getExternalStorageDirectory(),SystemClock.uptimeMillis()+".jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
Toast.makeText(getApplicationContext(), "成功", 0).show();
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});

}
/** 获取一个照相机实例 */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
@Override
protected void onDestroy() {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
super.onDestroy();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: