SurfaceView与Camera制作照相机
2011-10-08 09:20
267 查看
1.Activity:
package com.ccas.roadsideconnect;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class Takephotos extends Activity {
private CameraView cv;
private String photoPath;
private Camera mCamera = null;
public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// Matrix matrix = new Matrix();
// matrix.postScale(0.05f, 0.05f);
// Bitmap newBitmap=Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
File myCaptureFile = new File(getFilesDir(), photoPath);
try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(
myCaptureFile));
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
resetCamera();
Intent intent = new Intent();
intent.putExtra("photopath", myCaptureFile.getPath());
setResult(RESULT_OK, intent);
finish();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mBitmap != null) {
mBitmap.recycle();
}
}
}
};
//reret Camera
private void resetCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.takephoto);
// cv = new CameraView(this);
// FrameLayout fl = new FrameLayout(this);
// fl.addView(cv);
// setContentView(fl);
LinearLayout ll=(LinearLayout) findViewById(R.id.cameralayout);
cv = new CameraView(this);
ll.addView(cv);
Bundle extras=getIntent().getExtras();
if(extras!=null){
if(extras.getString("driver")!=null){
photoPath=extras.getString("driver");
}
else if(extras.getString("truck")!=null){
photoPath=extras.getString("truck");
}
}
//
ImageButton take=(ImageButton) findViewById(R.id.take);
take.getBackground().setAlpha(100);
take.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
}
});
// ImageButton cancel=(ImageButton) findViewById(R.id.cancel);
// cancel.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// finish();
// }
// });
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
} if(keyCode==KeyEvent.KEYCODE_BACK){
finish();
}
return cv.onKeyDown(keyCode, event);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
super.onPause();
resetCamera();
}
// set the Camera
class CameraView extends SurfaceView {
private SurfaceHolder holder = null;
public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
// Set images format
parameters.setPictureFormat(PixelFormat.JPEG);
// set Preview size
// WindowManager manager=(WindowManager) getSystemService(Context.WIFI_SERVICE);
// Display display=manager.getDefaultDisplay();
// parameters.setPreviewSize(320, 480);
// parameters.setPreviewSize(display.getWidth(), display.getHeight());
//set automatic focus
// parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
// Setting the picture the resolution of the preservation size
//parameters.setPictureSize(320, 240);
// set the data of Camera
mCamera.setParameters(parameters);
// start preview
mCamera.startPreview();
}
});
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
}
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/cameralayout" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="bottom|center" android:layout_gravity="bottom|center">
<ImageButton android:id="@+id/take" android:src="@drawable/takephoto"
android:layout_width="100dp" android:layout_height="50dp" />
<!--
<ImageButton android:id="@+id/cancel" android:src="@drawable/takephoto"
android:layout_width="100dp" android:layout_height="50dp" />
-->
</LinearLayout>
</FrameLayout>
package com.ccas.roadsideconnect;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class Takephotos extends Activity {
private CameraView cv;
private String photoPath;
private Camera mCamera = null;
public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// Matrix matrix = new Matrix();
// matrix.postScale(0.05f, 0.05f);
// Bitmap newBitmap=Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
File myCaptureFile = new File(getFilesDir(), photoPath);
try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(
myCaptureFile));
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
resetCamera();
Intent intent = new Intent();
intent.putExtra("photopath", myCaptureFile.getPath());
setResult(RESULT_OK, intent);
finish();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mBitmap != null) {
mBitmap.recycle();
}
}
}
};
//reret Camera
private void resetCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.takephoto);
// cv = new CameraView(this);
// FrameLayout fl = new FrameLayout(this);
// fl.addView(cv);
// setContentView(fl);
LinearLayout ll=(LinearLayout) findViewById(R.id.cameralayout);
cv = new CameraView(this);
ll.addView(cv);
Bundle extras=getIntent().getExtras();
if(extras!=null){
if(extras.getString("driver")!=null){
photoPath=extras.getString("driver");
}
else if(extras.getString("truck")!=null){
photoPath=extras.getString("truck");
}
}
//
ImageButton take=(ImageButton) findViewById(R.id.take);
take.getBackground().setAlpha(100);
take.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
}
});
// ImageButton cancel=(ImageButton) findViewById(R.id.cancel);
// cancel.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// finish();
// }
// });
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
} if(keyCode==KeyEvent.KEYCODE_BACK){
finish();
}
return cv.onKeyDown(keyCode, event);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
super.onPause();
resetCamera();
}
// set the Camera
class CameraView extends SurfaceView {
private SurfaceHolder holder = null;
public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
// Set images format
parameters.setPictureFormat(PixelFormat.JPEG);
// set Preview size
// WindowManager manager=(WindowManager) getSystemService(Context.WIFI_SERVICE);
// Display display=manager.getDefaultDisplay();
// parameters.setPreviewSize(320, 480);
// parameters.setPreviewSize(display.getWidth(), display.getHeight());
//set automatic focus
// parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
// Setting the picture the resolution of the preservation size
//parameters.setPictureSize(320, 240);
// set the data of Camera
mCamera.setParameters(parameters);
// start preview
mCamera.startPreview();
}
});
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
}
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/cameralayout" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="bottom|center" android:layout_gravity="bottom|center">
<ImageButton android:id="@+id/take" android:src="@drawable/takephoto"
android:layout_width="100dp" android:layout_height="50dp" />
<!--
<ImageButton android:id="@+id/cancel" android:src="@drawable/takephoto"
android:layout_width="100dp" android:layout_height="50dp" />
-->
</LinearLayout>
</FrameLayout>
相关文章推荐
- SurfaceView与Camera制作照相机
- 使用SurfaceView+Camera模仿照相机拍照功能
- Android自定义Camera,用SurfaceView预览。
- Opencv使用自带的CameraGLSurfaceView实现相机帧处理
- 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
- Android 自定义Camera之SurfaceView的使用(6.0权限申请)
- SurfaceView显示Camera摄像(之二设置参数、变焦)
- cocos2dx 3D战斗类游戏制作:【四】——一些零散笔记,3D小地图,android surfaceview等
- [置顶] Android SurfaceView+MediaPlayer制作播放暂停停止
- SurfaceView+Camera+OpenCv自定义相机保存图片
- Android的Camera用SurfaceView拍照
- 使用surfaceView制作视频播放器
- 通过Camera、SurfaceView、自己实现拍照及预览效果
- SurfaceView 教程 GIF 制作
- CameraSurfaceView——Android Camera使用详解
- Android学习笔记:使用SurfaceView制作简单游戏(1)
- 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
- 使用surfaceView制作的拍照demo
- 用SurfaceView制作简单的android游戏 : 重力小球(3)--------制作障碍物以及使其旋转
- Anroid TextureView、SurfaceView的Camera预览实现