8
回答
Android摄像头拍照程序的问题
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

程序运行在手机上的时候,会弹出一个警告框,提示程序异常停止,请重试!
下面是代码:
程序中只有一个java文件:MainActivity.java

package anvein.netCamera;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	Button takePic;
	final int WIDTH=400,HEIGHT=350;
	SurfaceView surfaceView;
	SurfaceHolder surfaceHolder;
	//定义系统所用的照相机
	Camera camera;
	//是否在浏览中
	boolean isPreview = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
//      获取拍照按钮
        takePic = (Button)findViewById(R.id.getPic);
        takePic.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(camera!=null){
					camera.takePicture(null, null, jpegCallback);
					System.out.println("is taking photo...");
				}
			}
		});
//      获取界面中SurfaceView组件
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
//      获取surfaceView中的SurfaceHolder
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.addCallback(new Callback(){

			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				// TODO Auto-generated method stub
				
			}

			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				initCamera();
			}

			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				//如果camera不为null,释放摄像头
				if(camera!=null){
					if(isPreview)
						camera.stopPreview();
					isPreview = false;
					camera.release();
					camera = null;
				}
			}
        	
        });
    }
    private void initCamera(){
    	if(camera==null && !isPreview){
    		//打开照相机
    		camera = Camera.open();
    	}
    	if(camera!=null && !isPreview){
    		try {
    			Camera.Parameters parmeters = camera.getParameters();
    			//设置照片预览的大小
    			parmeters.setPreviewSize(WIDTH, HEIGHT);
    			//每秒显示4帧
    			parmeters.setPreviewFrameRate(4);
    			//设置图片格式
    			parmeters.setPictureFormat(PixelFormat.JPEG);
    			//设置JPEG照片的质量
    			parmeters.set("jpeg-quality", 85);
    			//设置照片的大小
    			parmeters.setPictureSize(WIDTH, HEIGHT);
    			camera.setParameters(parmeters);
    			//通过surfaceView来显示取景画面
				camera.setPreviewDisplay(surfaceHolder);
		    	//开始预览
		    	camera.startPreview();
		    	isPreview = true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }
    PictureCallback jpegCallback = new PictureCallback(){

		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			System.out.println("get picture from camera!");
		}};
	
}


布局文件:
camera.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <SurfaceView 
           android:id="@+id/surface_camera"
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"
           android:layout_weight="1">
       </SurfaceView>
       <Button
              android:id="@+id/getPic"
              android:layout_width="100dip"
              android:layout_height="wrap_content"
              android:layout_gravity="left|bottom"
              android:text="拍照"
              android:padding="4px"
           />
       <ImageView 
           android:id="@+id/image1"
           android:layout_width="120dp"
           android:layout_height="105dp"
           android:adjustViewBounds="false"
           android:scaleType="centerInside"
           android:background="#0000ff"
           android:layout_margin="5dp"
           android:layout_gravity="right|top"
           />
       <ImageView 
           android:id="@+id/image2"
           android:layout_width="120dp"
           android:layout_height="105dp"
           android:adjustViewBounds="false"
           android:scaleType="centerInside"
           android:background="#0000ff"
           android:layout_margin="5dp"
           android:layout_gravity="right|bottom"
           />
    
    

</FrameLayout>


在AndroidManifest.xml中声明摄像头权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="anvein.netCamera"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <!-- 授权程序使用摄像头的权限 -->
    <uses-permission  android:name="android.permission.CAMERA"/>
    <uses-feature  android:name="android.hardware.camera"/>
    <uses-feature  android:name="android.hardware.camera.autofocus"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


程序调试的时候的logcat信息:

04-20 07:25:14.584: D/AndroidRuntime(278): Shutting down VM

04-20 07:25:14.584: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

04-20 07:25:14.664: E/AndroidRuntime(278): FATAL EXCEPTION: main

04-20 07:25:14.664: E/AndroidRuntime(278): java.lang.RuntimeException: setParameters failed

04-20 07:25:14.664: E/AndroidRuntime(278): at  android.hardware.Camera.native_setParameters(Native Method)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.hardware.Camera.setParameters(Camera.java:647)

04-20 07:25:14.664: E/AndroidRuntime(278): at anvein.netCamera.MainActivity.initCamera(MainActivity.java:93)

04-20 07:25:14.664: E/AndroidRuntime(278): at anvein.netCamera.MainActivity.access$0(MainActivity.java:75)

04-20 07:25:14.664: E/AndroidRuntime(278): at anvein.netCamera.MainActivity$3.surfaceCreated(MainActivity.java:58)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.SurfaceView.updateWindow(SurfaceView.java:532)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.View.draw(View.java:6743)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.widget.FrameLayout.draw(FrameLayout.java:352)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.View.draw(View.java:6743)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.widget.FrameLayout.draw(FrameLayout.java:352)

04-20 07:25:14.664: E/AndroidRuntime(278): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewRoot.draw(ViewRoot.java:1407)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)

04-20 07:25:14.664: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)

04-20 07:25:14.664: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)

04-20 07:25:14.664: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)

04-20 07:25:14.664: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

04-20 07:25:14.664: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

04-20 07:25:14.664: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)

举报
张安国
发帖于6年前 8回/7K+阅
共有8个答案 最后回答: 5年前

 java.lang.RuntimeException: setParameters failed

parmeters.setPictureSize(WIDTH, HEIGHT);(设置分辨率)的参数有误吧,如果不清楚分辨率可以却掉这句话,再运行看看。

--- 共有 1 条评论 ---
张安国嘿嘿!谢谢你啊,不愧大神,的确注释掉这句后程序运行正常了!不过我这里还有一问题,就是如果注释掉我就不能设置拍摄照片的大小了? 6年前 回复
04-23 15:26:42.407: ERROR/ThermalDaemon(247): Maximum CPU[1] frequency 1512000 KHz
04-23 15:26:42.627: ERROR/QualcommCameraHardware(4484): Maximum zoom value is 60
04-23 15:26:42.627: ERROR/QualcommCameraHardware(4484): FPS Range Values: 5000x31000
04-23 15:26:42.717: ERROR/MP-Decision(246): DOWN Ns:2.100000 Ts:270 rq:2.078431 seq:310.000000
04-23 15:26:42.717: ERROR/QualcommCameraHardware(4484): Invalid preview size requested: 400x350
04-23 15:26:42.727: ERROR/QualcommCameraHardware(4484): FPS Range Values: 5000x31000
04-23 15:26:42.737: ERROR/AndroidRuntime(7322): FATAL EXCEPTION: main
04-23 15:26:42.737: ERROR/AndroidRuntime(7322): java.lang.RuntimeException: setParameters failed
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.hardware.Camera.native_setParameters(Native Method)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.hardware.Camera.setParameters(Camera.java:1010)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at shouhulife.ca.CameraTest2Activity.initCamera(CameraTest2Activity.java:101)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at shouhulife.ca.CameraTest2Activity.access$0(CameraTest2Activity.java:83)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at shouhulife.ca.CameraTest2Activity$3.surfaceCreated(CameraTest2Activity.java:64)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.SurfaceView.updateWindow(SurfaceView.java:606)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:373)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.drawChild(ViewGroup.java:1666)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1387)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.drawChild(ViewGroup.java:1666)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1387)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.View.draw(View.java:6904)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.drawChild(ViewGroup.java:1668)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1387)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.drawChild(ViewGroup.java:1666)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1387)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.View.draw(View.java:6904)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1967)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewRoot.draw(ViewRoot.java:1538)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1262)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1875)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.os.Looper.loop(Looper.java:130)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at android.app.ActivityThread.main(ActivityThread.java:3703)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at java.lang.reflect.Method.invoke(Method.java:507)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-23 15:26:42.737: ERROR/AndroidRuntime(7322):     at dalvik.system.NativeStart.main(Native Method)
04-23 15:26:42.777: ERROR/dhcpcd(7275): timed out
04-23 15:26:42.787: ERROR/InputDispatcher(4489): channel '40760e10 shouhulife.ca/shouhulife.ca.CameraTest2Activity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
04-23 15:26:42.787: ERROR/InputDispatcher(4489): channel '40760e10 shouhulife.ca/shouhulife.ca.CameraTest2Activity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-23 15:26:42.787: ERROR/CAM_FD(4484): config_proc_CAMERA_STOP_SNAPSHOT: ctrl->state = 0
04-23 15:26:42.787: ERROR/CAM_FD(4484): cam_conf: CAMERA_EXIT
04-23 15:26:43.787: ERROR/WifiStateTracker(4489): DHCP request failed: DHCP result was failed
04-23 15:26:44.877: ERROR/ConnectivityService(4489): Attempt to connect to WIFI failed.
04-23 15:26:44.877: ERROR/ConnectivityService(4489): not failing over to mobile type 0 because Mobile Data Disabled
04-23 15:26:45.087: ERROR/MP-Decision(246): UP Nw:2.700000 Tw:180 rq:3.200000 seq:198.000000
04-23 15:26:45.407: ERROR/ThermalDaemon(247): Maximum CPU[1] frequency 1512000 KHz
04-23 15:26:45.827: ERROR/MP-Decision(246): DOWN Ns:2.100000 Ts:270 rq:1.000000 seq:319.000000

能帮我看看吗?

--- 共有 1 条评论 ---
张安国parmeters.setPreviewSize(WIDTH, HEIGHT); setPreviewFrameRate(4); setPictureFormat(PixelFormat.JPEG); set("jpeg-quality", 85); 你把这几句都注释掉试试吧,应该就可以了 6年前 回复

小弟刚刚接触Android,问个很基础的问题,摄像头的驱动程序怎么安装啊?如果有相关文档,能否给我一份?感激不尽!

我的邮箱:513438239@qq.com

顶部