cqolpix-尼康s2500拆机教程要调到摄相怎么调

Android Camera学习总结:如何在android中使用摄像头获取照片 - chlde2500 - 博客园
这学期补修了Android这门课,短短的八次课让我对Android有了初步的了解。作为结课项目,老师让我们用Camera/Surfaceview完成相机功能。现将学习的心得记录下来。
整个相机程序实现的思路是,使用Camera实例,设置好参数后,得到摄像头传回的图像数据,将这些数据在Surfaceview实例中进行展示,实现预览功能。在Surfaceview下,设置click button,当单击click button后,调用Camera.takePicture方法,完成照相动作,将图片保存到手机本地。
整个工程的核心是实现了SurfaceHolder.Callback接口,重写接口中的几个方法,而这几个方法,都是Surfaceview实例将要调用的关键方法。他们分别是:surfaceCreated/surfaceDestroyed/surfaceChanged。然后可以通过Surfaceview.getHolder().addCallback(Callback)方法,将这个Callback与前端layout中的surfaceview相关联起来。
下面来看下Callback中方法具体代码:
1 @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.e("tag", " surfaceChanged");
mParameters = camera.getParameters();
mParameters.setPictureFormat(PixelFormat.JPEG);
Log.e("tag",
"parameters.getPictureSize()"
+ mParameters.getPictureSize().width);
setPictureSize(mParameters);
Log.i("tag", "holder width:" + width + "
height:" + height);
// parameters.setPreviewSize(width, height);//需要判断支持的预览
camera.setParameters(mParameters);
camera.startPreview();
19 private void setPictureSize(Parameters parameters) {
List&Size& sizes = parameters.getSupportedPictureSizes();
if (sizes == null) {
int maxSize = 0;
int width = 0;
int height = 0;
for (int i = 0; i & sizes.size(); i++) {
Size size = sizes.get(i);
int pix = size.width * size.
if (pix & maxSize) {
width = size.
height = size.
Log.i("tag", "图片的大小:" + width + " height:" + height);
parameters.setPictureSize(width, height);
上段代码重写了surfaceChanged方法。该方法中完成了设置相机的参数,包括图片格式,预览尺寸,并开始预览功能,即在surfaceview中实时显示摄像头中捕捉的画面。其中setPicturesize方法使用了相对的图片大小设置,如果将图片大小设置成绝对值,可能会造成不同尺寸屏幕的机器不能正常运行程序的异常发生。
在另外两个方法中分别完成了camera实例的获取和销毁工作,具体代码如下:
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("tag", " surfaceDestroyed");
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
public void surfaceCreated(SurfaceHolder holder) {
if (camera == null) {
camera = Camera.open();
camera.setPreviewDisplay(holder);
Log.i("", "camera created");
//WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//Display display = wm.getDefaultDisplay();
Camera.Parameters parameters = camera.getParameters();
//parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 100);//照片质量
//parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面
camera.startPreview();//开始预览
} catch (IOException e) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
e.printStackTrace();
下面将整个项目的代码贡献出来,包括实现自动对焦,手动调焦距,设置闪光灯等功能。
共包含一个mainActivity和CameraCallback两个类
mainActivity的代码如下:
import android.app.A
import android.hardware.C
import android.os.B
import android.os.H
import android.os.M
import android.view.MotionE
import android.view.SurfaceH
import android.view.SurfaceV
import android.view.V
import android.view.View.OnClickL
import android.view.View.OnTouchL
import android.widget.B
import android.widget.SeekB
import android.widget.T
import android.widget.SeekBar.OnSeekBarChangeL
public class MainActivity extends Activity implements OnClickListener {
private Camera mC
private SurfaceView mSurfaceV
private SurfaceHolder mH
private CameraCallback mC
private Button mTakePicB
private Button mSwitchB
private Button mflashB
private boolean saved = true;
private boolean isFrontC
public static final int MESSAGE_SVAE_SUCCESS = 0;
public static final int MESSAGE_SVAE_FAILURE = 1;
private final int FLASH_MODE_AUTO = 0;
private final int FLASH_MODE_ON = 1;
private final int FLASH_MODE_OFF = 2;
private int mFlashMode = 0;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
saved = true;
switch (msg.what) {
case MESSAGE_SVAE_SUCCESS:
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT)
case MESSAGE_SVAE_FAILURE:
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT)
private SeekBar mZoomB
private View mZoomL
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
private void initView() {
mTakePicButton = (Button) findViewById(R.id.camera_take_btn);
mTakePicButton.setOnClickListener(this);
mSwitchButton = (Button) findViewById(R.id.camera_switch_btn);
mSwitchButton.setOnClickListener(this);
mflashButton = (Button) findViewById(R.id.flashMode);
mflashButton.setOnClickListener(this);
mZoomLayout = findViewById(R.id.zoomLayout);
mZoomBar = (SeekBar) findViewById(R.id.seekBar1);
mZoomBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
public void onStartTrackingTouch(SeekBar seekBar) {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mCallback.setZoom(progress);
initSurfaceView();
private void initSurfaceView() {
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
mHolder = mSurfaceView.getHolder();
mCallback = new CameraCallback(this);
mHolder.addCallback(mCallback);
mSurfaceView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP
&& !isFrontCamera) {// 前置摄像头取消触摸自动聚焦功能
View view = findViewById(R.id.RelativeLayout1);
mCallback.autoFocus(view, event);
return true;
// 判断是否支持前置摄像头
int cameras = mCallback.getNumberOfCameras();
if (cameras &= 1) {
mSwitchButton.setVisibility(View.GONE);
// 是否支持闪关灯
if (!mCallback.isSupportedFlashMode()) {
mflashButton.setVisibility(View.GONE);
if (mCallback.isSupportedZoom()) {
mZoomBar.setMax(mCallback.getMaxZoom());
mZoomBar.setVisibility(View.GONE);
public void onClick(View v) {
switch (v.getId()) {
case R.id.camera_take_btn:
if (saved) {
saved = false;
mCallback.takePicture(mHandler);
case R.id.camera_switch_btn:
isFrontCamera = !isFrontC
if (isFrontCamera) {
mSwitchButton.setText("打开后摄像头");
mflashButton.setVisibility(View.GONE);
mZoomLayout.setVisibility(View.GONE);
mSwitchButton.setText("打开前摄像头");
mflashButton.setVisibility(View.VISIBLE);
mZoomLayout.setVisibility(View.VISIBLE);
mCallback.switchCamera(mSurfaceView, isFrontCamera);
case R.id.flashMode:
mFlashMode = (mFlashMode + 1) % 3;
switch (mFlashMode) {
case FLASH_MODE_AUTO:
mflashButton.setText("flash_auto");
case FLASH_MODE_ON:
mflashButton.setText("flash_on");
case FLASH_MODE_OFF:
mflashButton.setText("flash_off");
mCallback.SetFlashMode(mFlashMode);
CameraCallback类代码如下:
import java.io.F
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import java.lang.reflect.M
import java.util.L
import android.app.A
import android.content.C
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.PixelF
import android.pressF
import android.hardware.C
import android.hardware.Camera.AutoFocusC //import android.hardware.Camera.CameraI
import android.hardware.Camera.P
import android.hardware.Camera.PictureC
import android.hardware.Camera.S
import android.os.E
import android.os.H
import android.util.L
import android.view.MotionE
import android.view.SurfaceH
import android.view.SurfaceV
import android.view.V
import android.view.animation.A
import android.view.animation.ScaleA
import android.view.animation.Animation.AnimationL
import android.widget.ImageV
import android.widget.RelativeL
import android.widget.RelativeLayout.LayoutP
public class CameraCallback implements SurfaceHolder.Callback {
private Context mC
private Camera mC
private boolean isShowF
private SurfaceHolder mH
// 在2.3的Camera.CameraInfo类中
// CAMERA_FACING_BACK常量的值为0,CAMERA_FACING_FRONT为1
private static final int CAMERA_FACING_BACK = 0;
private static final int CAMERA_FACING_FRONT = 1;
private final int FLASH_MODE_AUTO = 0;
private final int FLASH_MODE_ON = 1;
private final int FLASH_MODE_OFF = 2;
private Parameters mP
public CameraCallback(Context context) {
this.mContext =
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.e("tag", " surfaceChanged");
mParameters = mCamera.getParameters();
if (isSupportedFlashMode()) {// 需要判断是否支持闪光灯
mParameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
mParameters.setPictureFormat(PixelFormat.JPEG);
Log.e("tag",
"parameters.getPictureSize()"
+ mParameters.getPictureSize().width);
setPictureSize(mParameters);
// parameters.setPreviewFormat(PixelFormat.JPEG);//
Log.i("tag", "holder width:" + width + "
height:" + height);
// parameters.setPreviewSize(width, height);//需要判断支持的预览
mCamera.setParameters(mParameters);
mCamera.startPreview();
public void surfaceCreated(SurfaceHolder holder) {
Log.e("tag", " surfaceCreated");
if (mCamera == null) {
mCamera = Camera.open();
setDisplayOrientation(mCamera);
mCamera.setPreviewDisplay(holder);
Log.i("", "mCamera 2");
} catch (IOException e) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
e.printStackTrace();
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("tag", " surfaceDestroyed");
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
public int getNumberOfCameras() {
Method method = Camera.class.getMethod("getNumberOfCameras", null);
if (method != null) {
Object object = method.invoke(mCamera, null);
if (object != null) {
return (Integer)
} catch (Exception e) {
e.printStackTrace();
private void setDisplayOrientation(Camera camera) {
Method method = Camera.class.getMethod("setDisplayOrientation",
int.class);
if (method != null) {
method.invoke(camera, 90);
Log.i("tag", "方法名:" + method.getName());
} catch (Exception e) {
e.printStackTrace();
public Camera open(int i) {
Method method = Camera.class.getMethod("open", int.class);
if (method != null) {
Object object = method.invoke(mCamera, i);
if (object != null) {
return (Camera)
} catch (Exception e) {
e.printStackTrace();
return null;
// 设置图片大小
private void setPictureSize(Parameters parameters) {
List&Size& sizes = parameters.getSupportedPictureSizes();
if (sizes == null) {
int maxSize = 0;
int width = 0;
int height = 0;
for (int i = 0; i & sizes.size(); i++) {
Size size = sizes.get(i);
int pix = size.width * size.
if (pix & maxSize) {
width = size.
height = size.
Log.i("tag", "图片的大小:" + width + " height:" + height);
parameters.setPictureSize(width, height);
public Camera getCamera() {
// 自动对焦
public void autoFocus(View v, MotionEvent event) {
if (isShowFrame) {
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
RelativeLayout layout = (RelativeLayout)
final ImageView imageView = new ImageView(mContext);
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.lcamera_focus_frame1);
imageView.setImageBitmap(bitmap);
LayoutParams params = new RelativeLayout.LayoutParams(
bitmap.getWidth(), bitmap.getHeight());
// imageView.setLayoutParams(params);
Log.e("tag", "bitmap.getWidth:" + bitmap.getWidth());
params.leftMargin = (int) (event.getX() - bitmap.getWidth() / 2);
params.topMargin = (int) (event.getY() - bitmap.getHeight() / 2);
layout.addView(imageView, params);
imageView.setVisibility(View.VISIBLE);
ScaleAnimation animation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
animation.setDuration(300);
animation.setFillAfter(true);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
public void onAnimationRepeat(Animation animation) {
public void onAnimationEnd(final Animation animation) {
imageView.setImageResource(R.drawable.lcamera_focus_frame2);
new Thread() {
public void run() {
Thread.sleep(400);
((Activity) (mContext))
.runOnUiThread(new Runnable() {
public void run() {
.setImageResource(R.drawable.lcamera_focus_frame3);
Thread.sleep(200);
((Activity) (mContext))
.runOnUiThread(new Runnable() {
public void run() {
imageView.clearAnimation();
imageView.setVisibility(View.GONE);
isShowFrame = false;
} catch (InterruptedException e) {
e.printStackTrace();
}.start();
imageView.startAnimation(animation);
isShowFrame = true;
public void takePicture(final Handler handler) {
mCamera.takePicture(null, null, new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
directory = new File(Environment
.getExternalStorageDirectory(), "camera");
directory = new File(mContext.getCacheDir(), "camera");
if (!directory.exists()) {
directory.mkdir();
File file = new File(directory, System.currentTimeMillis()
+ ".jpg");
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
fos = new FileOutputStream(file);
boolean compress = press(CompressFormat.JPEG,
100, fos);
if (compress) {
handler.sendEmptyMessage(MainActivity.MESSAGE_SVAE_SUCCESS);
handler.sendEmptyMessage(MainActivity.MESSAGE_SVAE_FAILURE);
mCamera.startPreview();
Log.i("tag", " 保存是否成功:" + compress + "
file.exists:"
+ file.exists());
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
} catch (IOException e) {
// 多镜头切换
public void switchCamera(SurfaceView surfaceView, boolean isFrontCamera) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
int cameraId = isFrontCamera ? CAMERA_FACING_FRONT : CAMERA_FACING_BACK;// CAMERA_FACING_FRONT为前置摄像头
mCamera = open(cameraId);
Parameters parameters = mCamera.getParameters();
Log.e("tag",
"parameters.getPictureSize()"
+ parameters.getPictureSize().width);
setPictureSize(parameters);
parameters.setPictureFormat(PixelFormat.JPEG);
mCamera.setParameters(parameters);
Log.e("tag",
"2 parameters.getPictureSize()"
+ parameters.getPictureSize().width);
setDisplayOrientation(mCamera);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
public boolean isSupportedZoom() {
if (mCamera != null) {
Parameters parameters = mCamera.getParameters();
return parameters.isZoomSupported();
return false;
public int getMaxZoom() {
if (mCamera == null) {
mCamera = Camera.open();
mParameters = mCamera.getParameters();
return mParameters.getMaxZoom();
// 设置Zoom
public void setZoom(int value) {
Log.i("tag", "value:" + value);
mParameters.setZoom(value);
mCamera.setParameters(mParameters);
mCamera.startPreview();
public boolean isSupportedFlashMode() {
if (mCamera == null) {
mCamera = Camera.open();
Parameters parameters = mCamera.getParameters();
List&String& modes = parameters.getSupportedFlashModes();
if (modes != null && modes.size() != 0) {
boolean autoSupported = modes.contains(Parameters.FLASH_MODE_AUTO);
boolean onSupported = modes.contains(Parameters.FLASH_MODE_ON);
boolean offSupported = modes.contains(Parameters.FLASH_MODE_OFF);
return autoSupported && onSupported && offS
return false;
// 设置闪光灯模式
public void SetFlashMode(int flashMode) {
switch (flashMode) {
case FLASH_MODE_AUTO:
mParameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
case FLASH_MODE_ON:
mParameters.setFlashMode(Parameters.FLASH_MODE_ON);
case FLASH_MODE_OFF:
mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mParameters);
mCamera.startPreview();
layout中main.xml声明相应的surfaceview和相应的button/seekbar
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" &
&SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="55dp" /&
&RelativeLayout
android:id="@+id/camera_bottom"
android:layout_width="fill_parent"
android:layout_height="55.0dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#88ffffff"
android:orientation="vertical" &
android:id="@+id/camera_take_btn"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="3.0dip"
android:text="拍照" /&
&/RelativeLayout&
android:id="@+id/camera_switch_btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="打开前置摄像头" &
android:id="@+id/flashMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:text="flash_auto" /&
&LinearLayout
android:id="@+id/zoomLayout"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_above="@+id/camera_bottom"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:orientation="horizontal" &
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
android:textColor="#ffffff" /&
android:id="@+id/seekBar1"
android:layout_width="250dp"
android:layout_height="13dp"
android:layout_gravity="center_vertical"
android:maxHeight="6dp"
android:progressDrawable="@drawable/seekbar_progress"
android:thumb="@drawable/camera_seekbar_progress_ball"
android:thumbOffset="0dp" /&
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="top"
android:text=" + "
android:textColor="#ffffff" /&
&/LinearLayout&
&/RelativeLayout&
在AndroidManifest中做如下权限声明
&uses-sdk android:minSdkVersion="8" /&
&uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&
&uses-permission android:name="android.permission.CAMERA" /&
&uses-feature android:name="android.hardware.camera" /&
&uses-feature android:name="android.hardware.camera.autofocus" /&我用尼康 colpix s2500 录的视频传到电脑,我用了好几种播放器。为什么老是播放不了,_百度知道
我用尼康 colpix s2500 录的视频传到电脑,我用了好几种播放器。为什么老是播放不了,
我有更好的答案
这个你的是什么格式的啊你要有相关的解码才行的
其他类似问题
为您推荐:
您可能关注的推广
尼康的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 hisome s2500调试 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信