java捕捉摄像头画面_使用摄像头捕获图片

在main.xml中:

xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:background=“#000000”>

android:id=“@+id/but”

android:layout_width=“50dp”

android:layout_height=“fill_parent”

android:background=“#3399ff”

android:textColor=“#ffffff”

android:text=“照相”/>

android:id=“@+id/surface”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”/>

在MyCameraDemo.java中:

package com.li.camera;

import java.io.IOException;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Context;

import android.graphics.PixelFormat;

import android.hardware.Camera;

import android.hardware.Camera.Parameters;

import android.os.Bundle;

import android.view.Display;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.WindowManager;

import android.widget.Button;

public class MyCameraDemo extends Activity {

private SurfaceView surface = null ;

private Button but = null ;

private SurfaceHolder holder = null ;

private Camera cam = null ;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

this.but = (Button) super.findViewById(R.id.but) ;

this.surface = (SurfaceView) super.findViewById(R.id.surface) ;

this.holder = this.surface.getHolder() ;

this.holder.addCallback(new MySurfaceViewCallback()) ;

this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) ;

this.holder.setFixedSize(800, 480);

this.but.setOnClickListener(new OnClickListenerImpl()) ;

}

private class OnClickListenerImpl implements OnClickListener {

public void onClick(View v) {

}

}

private class MySurfaceViewCallback implements SurfaceHolder.Callback {

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

}

@SuppressLint(“NewApi”)

public void surfaceCreated(SurfaceHolder holder) {

MyCameraDemo.this.cam = Camera.open(0) ;  // 取得第一个摄像头

WindowManager manager = (WindowManager) MyCameraDemo.this

.getSystemService(Context.WINDOW_SERVICE);

Display display = manager.getDefaultDisplay() ;

Parameters param = MyCameraDemo.this.cam.getParameters() ;

param.setPreviewSize(display.getWidth(), display.getHeight()) ;

param.setPreviewFrameRate(5) ; // 一秒5帧

param.setPictureFormat(PixelFormat.JPEG) ;  // 图片形式

param.set(“jpen-quality”, 85) ;

MyCameraDemo.this.cam.setParameters(param) ;

try {

MyCameraDemo.this.cam.setPreviewDisplay(MyCameraDemo.this.holder) ;

} catch (IOException e) {

}

MyCameraDemo.this.cam.startPreview() ;  // 进行预览

}

public void surfaceDestroyed(SurfaceHolder holder) {

}

}

}

修改AndroidManifest.xml中:

package=“com.li.camera”

android:versionCode=“1”

android:versionName=“1.0” >

android:minSdkVersion=“8”

android:targetSdkVersion=“15” />

android:icon=“@drawable/ic_launcher”

android:label=“@string/app_name”

android:theme=“@style/AppTheme” >

android:name=“.MyCameraDemo”

android:label=“@string/title_activity_my_camera_demo”

android:screenOrientation=“landscape”>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值