CustomerCamera的代码
package com.first.mylineme.util;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import com.example.mylineme.R;
import com.first.mylineme.ui.ResultAty;
public class CustomerCamera extends Activity implements SurfaceHolder.Callback{
private Camera xcamera;
private SurfaceView xPreview;
private SurfaceHolder xHolder;
private Camera.PictureCallback xPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File tempFile = new File("/sdcard/temp.png");
try {
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(data);
fos.close();
Intent intent = new Intent(CustomerCamera.this, ResultAty.class);
intent.putExtra("picPath", tempFile.getAbsolutePath());
startActivity(intent);
CustomerCamera.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_camera);
xPreview = (SurfaceView) findViewById(R.id.preview);
xHolder = xPreview.getHolder();
xHolder.addCallback(this);
xPreview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
xcamera.autoFocus(null);
}
});
}
public void capture(View view){
Camera.Parameters parameters = xcamera.getParameters();
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setPictureSize(800, 400);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
xcamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if(success){
xcamera.takePicture(null, null, xPictureCallback);
}
}
});
}
/*
* 获取Camera对象
*/
private Camera getCamera(){
Camera camera;
try {
camera = Camera.open();
} catch (Exception e) {
camera = null;
}
return camera;
}
/*
* 开始预览相机内容
*/
private void setStartPreview(Camera camera,SurfaceHolder surfaceHolder){
try {
camera.setPreviewDisplay(surfaceHolder);
//将系统Camera预览角度进行调整
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 释放相机资源
*/
private void releaseCamera(){
if(xcamera!=null){
xcamera.setPreviewCallback(null);
xcamera.stopPreview();
xcamera.release();
xcamera = null;
}
}
@Override
protected void onResume() {
super.onResume();
if(xcamera == null){
xcamera = getCamera();
if(xHolder!=null){
setStartPreview(xcamera,xHolder);
}
}
}
@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
setStartPreview(xcamera, xHolder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
xcamera.stopPreview();
setStartPreview(xcamera, xHolder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
}
ResultAty的代码
package com.first.mylineme.ui;
import java.io.FileInputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;
import com.example.mylineme.R;
public class ResultAty extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
String path = getIntent().getStringExtra("picPath");
ImageView imageView = (ImageView) findViewById(R.id.pic);
try {
FileInputStream fis = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
Matrix matrix = new Matrix();
matrix.setRotate(90);
bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),
bitmap.getHeight(),matrix,true);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
// Bitmap bitmap = BitmapFactory.decodeFile(path);
// imageView.setImageBitmap(bitmap);
}
}
result的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Capture result"
android:gravity="center"
android:textSize="30sp"/>
<ImageView
android:id="@+id/pic"
android:scaleType="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
customer_camera的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Capture"
android:onClick="capture"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:text="Welcome to my camera"
android:textColor="#4682B4"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>