权限
<uses-permission android:name="android.permission.CAMERA" />
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="camera.com.testsurfaceview.MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:layout_height="match_parent" />
</RelativeLayout>
使用
package camera.com.testsurfaceview;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceceHolder;
private Camera camera;
private ImageView imageView;
private int viewWidth,viewHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
imageView = (ImageView) findViewById(R.id.imageView);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mSurfaceceHolder = mSurfaceView.getHolder();
mSurfaceceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
initCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.release();
}
}
});
mSurfaceView.setOnClickListener(this);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (mSurfaceView != null) {
viewWidth = mSurfaceView.getWidth();
viewHeight = mSurfaceView.getHeight();
}
}
private void initCamera() {
camera = Camera.open();
camera.setDisplayOrientation(90);
if(camera!=null){
try {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewFpsRange(viewWidth, viewHeight);
parameters.setPictureFormat(ImageFormat.JPEG);
//通过surfaceView显示预览
camera.setPreviewDisplay(mSurfaceceHolder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
if(camera==null)return;
camera.autoFocus(autoFocusCallback);
}
Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback(){
@Override
public void onAutoFocus(boolean success, Camera camera) {
if(success){ //对焦成功
camera.takePicture(new Camera.ShutterCallback() {
@Override
public void onShutter() {
//按下快门
}
}, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {//是否保存原始图片的信息
}
},pictureCallback);
}
}
};
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
final Bitmap resource = BitmapFactory.decodeByteArray(data, 0, data.length);
if(resource ==null){
Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
}
final Matrix matrix = new Matrix();
matrix.setRotate(90);
final Bitmap bitmap = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), matrix, true);
if (bitmap != null && imageView != null && imageView.getVisibility() == View.GONE) {
camera.stopPreview();
imageView.setVisibility(View.VISIBLE);
mSurfaceView.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(bitmap);
}
}
};
}
Camera数据得图像处理。
/**
* 现场照片转化
*/
byte[] data = zzrFaceBean2.getCurrentData();
YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, mWidth, mHeight, null);//data是onPreviewFrame参数提供
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, yuvimage.getWidth(), yuvimage.getHeight()), 100, baos);// 80--JPG图片的质量[0-100],100最高
byte[] rawImage = baos.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inPreferredConfig = Bitmap.Config.RGB_565; //默认8888
//options.inSampleSize = 8;
SoftReference<Bitmap> softRef = new SoftReference<>(BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options));//方便回收
Bitmap bitmap = softRef.get();
这种效率更高
private RenderScript rs;
private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
private Type.Builder yuvType, rgbaType;
private Allocation in, out;
/**
* 现场照片转化
*/
byte[] data = zzrFaceBean2.getCurrentData();
rs = RenderScript.create(this);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
// if (yuvType == null) {
yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length);
in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(mWidth).setY(mHeight);
out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
// }
in.copyFrom(data);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
Bitmap bmpout = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);
matchCurrent.setImageBitmap(bmpout);