您可以使用SurfaceView
来捕获图像
package com.camera;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Camera_capture extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Button capture_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
capture_image = (Button) findViewById(R.id.capture_image);
capture_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
capture();
}
});
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(Camera_capture.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
private void capture() {
mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Toast.makeText(getApplicationContext(), "Picture Taken",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("image_arr", data);
setResult(RESULT_OK, intent);
camera.stopPreview();
if (camera != null) {
camera.release();
mCamera = null;
}
finish();
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.e("Surface Changed", "format == " + format + ", width === "
+ width + ", height === " + height);
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e("Surface Created", "");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("Surface Destroyed", "");
}
@Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
}
布局文件将是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_weight="100"
android:layout_height="wrap_content" />
<Button
android:id="@+id/capture_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capture" />
</LinearLayout>
开始此Camera_capture
活动,startActivityForResult
您onActivityResult
可以将图像作为byte
数组获取
byte[] image = data.getExtras().getByteArray("image_arr");
data
接收到的数据在哪里。
解码byte
数组以Bitmap
使用
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
image.length);
现在设置这个Bitmap
编辑
由于返回时出现问题byte[]
,byte[]
应将文件保存在文件中,并将文件路径发送到前一个Activity
文件,以便读取文件。
中onPictureTaken()
,只需添加
String PATH = "Any path to store a file";
try {
FileOutputStream fos=new FileOutputStream(PATH);
fos.write(data);
fos.close();
}
catch (java.io.IOException e) {
}
并代替:
intent.putExtra("image_arr", data);
写
intent.putExtra("image_path", PATH);
Activity
并在以前的中接收此路径onActivityResult
:
String imagePath = data.getExtras().getString("image_path");