package com.yu.com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
public class ComputeImageActivity extends Activity
{
SurfaceView sView;
SurfaceHolder surfaceHolder;
int screenWidth, screenHeight;
// 定义系统所用的照相机
Camera camera;
//是否在浏览中
boolean isPreview = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
WindowManager wm = (WindowManager) getSystemService(
Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
// 获取屏幕的宽和高
screenWidth = display.getWidth();
screenHeight = display.getHeight();
// 获取界面中SurfaceView组件
sView = (SurfaceView) findViewById(R.id.sView);
// 获得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)
{
// 打开摄像头
initCamera();
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// 如果camera不为null ,释放摄像头
if (camera != null)
{
if (isPreview)
camera.stopPreview();
camera.release();
camera = null;
}
}
});
// 设置该SurfaceView自己不维护缓冲
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} private void initCamera()
{
if (!isPreview)
{
camera = Camera.open();
}
if (camera != null && !isPreview)
{
try
{
Camera.Parameters parameters = camera.getParameters();
// 设置预览照片的大小
parameters.setPreviewSize(screenWidth, screenHeight);
// 每秒显示4帧
parameters.setPreviewFrameRate(4);
// 设置图片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 设置JPG照片的质量
parameters.set("jpeg-quality", 85);
//设置照片的大小
parameters.setPictureSize(screenWidth, screenHeight);
camera.setParameters(parameters);
//通过SurfaceView显示取景画面
camera.setPreviewDisplay(surfaceHolder);
// 开始预览
camera.startPreview();
// 自动对焦
camera.autoFocus(null);
}
catch (Exception e)
{
e.printStackTrace();
}
isPreview = true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
// 当用户单击照相键、中央键时执行拍照
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_CAMERA:
if (camera != null && event.getRepeatCount() == 0)
{
// 拍照
camera.takePicture(null, null , myjpegCallback);
return true;
}
break;
}
return super.onKeyDown(keyCode, event);
}
PictureCallback myjpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
// 根据拍照所得的数据创建位图
final Bitmap bm = BitmapFactory.decodeByteArray(data
, 0, data.length);
// 加载/layout/save.xml文件对应的布局资源
View saveDialog = getLayoutInflater().inflate(
R.layout.save, null);
final EditText photoName = (EditText) saveDialog
.findViewById(R.id.phone_name);
// 获取saveDialog对话框上的ImageView组件
ImageView show = (ImageView) saveDialog.findViewById(R.id.show);
// 显示刚刚拍得的照片
show.setImageBitmap(bm);
//使用对话框显示saveDialog组件
new AlertDialog.Builder(ComputeImageActivity.this)
.setView(saveDialog)
.setPositiveButton("保存", new OnClickListener()
{
public void onClick(DialogInterface dialog,
int which)
{
// 创建一个位于SD卡上的文件
File file = new File(Environment.getExternalStorageDirectory()
, photoName.getText().toString() + ".jpg");
FileOutputStream outStream = null;
try
{
// 打开指定文件对应的输出流
outStream = new FileOutputStream(file);
// 把位图输出到指定文件中
bm.compress(CompressFormat.JPEG, 100, outStream);
outStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
})
.setNegativeButton("取消", null)
.show();
//重新浏览
camera.stopPreview();
camera.startPreview();
isPreview = true;
}
};
}布局文件:
main.xml:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="@+id/sView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>sava.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:text="名字"
/>
<!-- 定义一个文本框来让用户输入照片名 -->
<EditText
android:id="@+id/phone_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<!-- 定义一个图片框来显示照片 -->
<ImageView
android:id="@+id/show"
android:layout_width="240px"
android:layout_height="315px"
android:scaleType="fitCenter"
android:layout_marginTop="10dp"/>
</LinearLayout>android
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
public class ComputeImageActivity extends Activity
{
SurfaceView sView;
SurfaceHolder surfaceHolder;
int screenWidth, screenHeight;
// 定义系统所用的照相机
Camera camera;
//是否在浏览中
boolean isPreview = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
WindowManager wm = (WindowManager) getSystemService(
Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
// 获取屏幕的宽和高
screenWidth = display.getWidth();
screenHeight = display.getHeight();
// 获取界面中SurfaceView组件
sView = (SurfaceView) findViewById(R.id.sView);
// 获得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)
{
// 打开摄像头
initCamera();
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// 如果camera不为null ,释放摄像头
if (camera != null)
{
if (isPreview)
camera.stopPreview();
camera.release();
camera = null;
}
}
});
// 设置该SurfaceView自己不维护缓冲
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} private void initCamera()
{
if (!isPreview)
{
camera = Camera.open();
}
if (camera != null && !isPreview)
{
try
{
Camera.Parameters parameters = camera.getParameters();
// 设置预览照片的大小
parameters.setPreviewSize(screenWidth, screenHeight);
// 每秒显示4帧
parameters.setPreviewFrameRate(4);
// 设置图片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 设置JPG照片的质量
parameters.set("jpeg-quality", 85);
//设置照片的大小
parameters.setPictureSize(screenWidth, screenHeight);
camera.setParameters(parameters);
//通过SurfaceView显示取景画面
camera.setPreviewDisplay(surfaceHolder);
// 开始预览
camera.startPreview();
// 自动对焦
camera.autoFocus(null);
}
catch (Exception e)
{
e.printStackTrace();
}
isPreview = true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
// 当用户单击照相键、中央键时执行拍照
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_CAMERA:
if (camera != null && event.getRepeatCount() == 0)
{
// 拍照
camera.takePicture(null, null , myjpegCallback);
return true;
}
break;
}
return super.onKeyDown(keyCode, event);
}
PictureCallback myjpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
// 根据拍照所得的数据创建位图
final Bitmap bm = BitmapFactory.decodeByteArray(data
, 0, data.length);
// 加载/layout/save.xml文件对应的布局资源
View saveDialog = getLayoutInflater().inflate(
R.layout.save, null);
final EditText photoName = (EditText) saveDialog
.findViewById(R.id.phone_name);
// 获取saveDialog对话框上的ImageView组件
ImageView show = (ImageView) saveDialog.findViewById(R.id.show);
// 显示刚刚拍得的照片
show.setImageBitmap(bm);
//使用对话框显示saveDialog组件
new AlertDialog.Builder(ComputeImageActivity.this)
.setView(saveDialog)
.setPositiveButton("保存", new OnClickListener()
{
public void onClick(DialogInterface dialog,
int which)
{
// 创建一个位于SD卡上的文件
File file = new File(Environment.getExternalStorageDirectory()
, photoName.getText().toString() + ".jpg");
FileOutputStream outStream = null;
try
{
// 打开指定文件对应的输出流
outStream = new FileOutputStream(file);
// 把位图输出到指定文件中
bm.compress(CompressFormat.JPEG, 100, outStream);
outStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
})
.setNegativeButton("取消", null)
.show();
//重新浏览
camera.stopPreview();
camera.startPreview();
isPreview = true;
}
};
}布局文件:
main.xml:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="@+id/sView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>sava.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:text="名字"
/>
<!-- 定义一个文本框来让用户输入照片名 -->
<EditText
android:id="@+id/phone_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<!-- 定义一个图片框来显示照片 -->
<ImageView
android:id="@+id/show"
android:layout_width="240px"
android:layout_height="315px"
android:scaleType="fitCenter"
android:layout_marginTop="10dp"/>
</LinearLayout>android
解决方案 »
- android 游戏 血战钓鱼岛。、、、谢谢支持
- 三个竖直LinearLayout 布局的问题,在线等
- 对话框如何传递参数到mainActivity?
- MediaPlayer的小白问题,标题要长~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 如何辨别该流量是WIFI产生还是GPS产生呀?
- 在控件中显示文字遇到的TextView问题,TextView不能被解析成一个变量
- 显示界面和运行界面不一致
- Android 模拟器 No Service的问题
- Android 中传递 json 的问题
- 新手练习求救 fragment里面获取布局问题
- 怎么访问assets文件夹下的数据库文件???
- android soap2协议访问webservice的问题
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
public void init(){
shutterButton = (Button) findViewById(R.id.takepicture);
shutterButton.setOnClickListener(this);
// switch camera id
switchCameraId = (Button)findViewById(R.id.switchcameraid);
switchCameraId.setOnClickListener(this);
//switch mode
switchMode = (Button)findViewById(R.id.switchmode);
switchMode.setOnClickListener(this);
mSurfaceView = (SurfaceView)this.findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.setKeepScreenOn(true);
mSurfaceHolder.addCallback(new SurfaceCallback());
imageButton = (ImageButton)findViewById(R.id.imageButton1);
}
private class SurfaceCallback implements SurfaceHolder.Callback{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
Log.v("shenwenjian","aaaaaaaaaaaaaaaa");
mSurfaceHolder2 = arg0;
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mCamera = Camera.open();
Log.v("shenwenjian", "cameranumber"+mCamera.getNumberOfCameras());
if(mCamera == null){
Toast.makeText(getApplicationContext(), "open faile", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "open success", Toast.LENGTH_SHORT).show();
Camera.Parameters mParameters = mCamera.getParameters();
mParameters.setPictureSize(640, 480);
mParameters.setPreviewSize(320, 240);
Toast.makeText(getApplicationContext(), "isSmoothZoomSupported"+mParameters.isSmoothZoomSupported(), 1).show();
//Toast.makeText(getApplicationContext(), ""+mParameters.getMaxNumDetectedFaces(), 0).show();
//test
// Log.v("shenwenjian", "current:"+mParameters.getZoom()+"max:"+mParameters.getMaxZoom()+"picturesize:" +
// mParameters.getPictureSize().height+"+"+mParameters.getPictureSize().width
// +"previewsize"+mParameters.getPreviewSize().height+"+"+mParameters.getPreviewSize().width);
List<Size> sizes = mParameters.getSupportedPictureSizes();
Iterator it = sizes.iterator();
while(it.hasNext()){
Size size = (Size)it.next();
Log.v("shenwenjian", "width"+size.width+"+height"+size.height);
}
//mParameters.setPictureSize(arg0, arg1)
// mParameters.setZoom(mParameters.getMaxZoom());
//mParameters.setExposureCompensation(0);
mParameters.setExposureCompensation(new Integer("1"));
Toast.makeText(getApplicationContext(), "max:"+mParameters.getMaxExposureCompensation()+" min:"+mParameters.getMinExposureCompensation()+" step:"+ mParameters.getExposureCompensationStep(), 1).show();
Log.v("shenwejian","max:"+mParameters.getMaxExposureCompensation()+" min:"+mParameters.getMinExposureCompensation()+"step:"+ mParameters.getExposureCompensationStep());
mParameters.setZoom(0);
mParameters.setRotation(90);
//mParameters.setPictureSize(3200, 2400);
Log.v("shenwnejian", ""+mParameters.flatten());
//mCamera.setDisplayOrientation(mDisplayRotation); android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
//android.hardware.Camera.getCameraInfo(cameraId, info);
mCamera.getCameraInfo(0, info);
int rotation = getWindowManager().getDefaultDisplay()
.getRotation();
Log.v("shenwenjian","rotation= "+rotation);
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
Log.v("shenwenjian","info.orientation:"+info.orientation + " degrees: "+degrees );
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
Log.v("shenwenjian","info"+info.orientation);
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
Log.v("shenwenjian","result: "+result);
//mCamera.setDisplayOrientation(result);
Toast.makeText(getApplicationContext(), "mCamera.setDisplayOrientation(result);", 0).show();
mCamera.setParameters(mParameters);
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "setPreviewDisplay faile!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
mCamera.startPreview();
Log.v("shenwenjian", mParameters.flatten());
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.v("shenwenjian", "camera surfaceDestroy");
}
}