Wednesday, April 23, 2014

Some questions about camera android development


Recent development in camera android program has encountered problems unresolved, seeking help
heroesCode is as follows:
public class CameraTActivity extends Activity implements OnClickListener, SurfaceHolder.Callback {
/ ** Called when the activity is first created. * /
private Button camera_start, camera_pause;
public Camera camera;
public Camera.Parameters parameters;

private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
private SurfaceView camera_play_surfaceview;
private int screenWidth, screenHeight;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);

initView ();
System.out.println ("start ---------");
}
@ Override
public void surfaceChanged (SurfaceHolder holder, int format, int width,
int height) {
/ / TODO Auto-generated method stub
try {
startCamera ();
} Catch (Exception e) {
/ / TODO: handle exception
e.printStackTrace ();
}
}
@ Override
public void surfaceCreated (SurfaceHolder holder) {
/ / TODO Auto-generated method stub
System.out.println ("create");
if (camera == null) {
camera = Camera.open ();
}
System.out.println ("camera start");
}
@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
/ / TODO Auto-generated method stub
if (camera! = null) {
camera.stopPreview ();
camera.release ();

}
}
public void initView () {

surfaceView = (SurfaceView) findViewById (R.id.surfaceview);
surfaceHolder = surfaceView.getHolder ();
surfaceHolder.addCallback (this);

}
/ **
* Creates a new Camera object to access the first back-facing camera on the device.
* If the device does not have a back-facing camera, this returns null.
* @ Throws IOException * /
private void startCamera () throws IOException {


parameters = camera.getParameters ();
screenWidth = surfaceView.getWidth ();
screenHeight = surfaceView.getHeight ();
parameters.setPreviewSize (screenWidth, screenHeight); / / Set the size of the preview pictures
parameters.setPreviewFpsRange (60, 75); / / display the second 20 to 30
parameters.setPictureFormat (ImageFormat.NV21); / / Format Picture
parameters.setPictureSize (screenWidth, screenHeight); / / set the photo size
/ / Camera.setParameters (parameters); / / android2.3.3 do not need this line of code after
camera.setPreviewDisplay (surfaceHolder); / / Display Viewfinder screen
by SurfaceViewcamera.setPreviewCallback (cPreview); / / Set the callback class
camera.startPreview (); / / start preview
camera.autoFocus (null); / / AF
}
}

log information is as follows:

01-02 01:21:13.380: WARN / System.err (3155): java.lang.RuntimeException: startPreview failed
01-02 01:21:13.390: WARN / System.err (3155): at android.hardware.Camera.startPreview (Native Method)
01-02 01:21:13.390: WARN / System.err (3155): at com.camera.CameraTActivity.startCamera (CameraTActivity.java: 100)
01-02 01:21:13.390: WARN / System.err (3155): at com.camera.CameraTActivity.surfaceChanged (CameraTActivity.java: 40)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.SurfaceView.updateWindow (SurfaceView.java: 549)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.SurfaceView.dispatchDraw (SurfaceView.java: 348)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.drawChild (ViewGroup.java: 1645)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.dispatchDraw (ViewGroup.java: 1374)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.drawChild (ViewGroup.java: 1645)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.dispatchDraw (ViewGroup.java: 1374)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.View.draw (View.java: 7165)
01-02 01:21:13.390: WARN / System.err (3155): at android.widget.FrameLayout.draw (FrameLayout.java: 357)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.drawChild (ViewGroup.java: 1647)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.dispatchDraw (ViewGroup.java: 1374)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.drawChild (ViewGroup.java: 1645)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewGroup.dispatchDraw (ViewGroup.java: 1374)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.View.draw (View.java: 7165)
01-02 01:21:13.390: WARN / System.err (3155): at android.widget.FrameLayout.draw (FrameLayout.java: 357)
01-02 01:21:13.390: WARN / System.err (3155): at com.android.internal.policy.impl.PhoneWindow $ DecorView.draw (PhoneWindow.java: 1862)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewRoot.draw (ViewRoot.java: 1592)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewRoot.performTraversals (ViewRoot.java: 1301)
01-02 01:21:13.390: WARN / System.err (3155): at android.view.ViewRoot.handleMessage (ViewRoot.java: 1931)
01-02 01:21:13.390: WARN / System.err (3155): at android.os.Handler.dispatchMessage (Handler.java: 99)
01-02 01:21:13.390: WARN / System.err (3155): at android.os.Looper.loop (Looper.java: 130)
01-02 01:21:13.390: WARN / System.err (3155): at android.app.ActivityThread.main (ActivityThread.java: 3683)
01-02 01:21:13.390: WARN / System.err (3155): at java.lang.reflect.Method.invokeNative (Native Method)
01-02 01:21:13.390: WARN / System.err (3155): at java.lang.reflect.Method.invoke (Method.java: 507)
01-02 01:21:13.390: WARN / System.err (3155): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 839)
01-02 01:21:13.390: WARN / System.err (3155): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 597)
01-02 01:21:13.390: WARN / System.err (3155): at dalvik.system.NativeStart.main (Native Method)
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
private Button camera_start, camera_pause; maybe you are not the find button next?

Reply:
Camera.open is successful it? Camera's permission add it?
Reply:
at android.view.ViewRoot.handleMessage (ViewRoot.java: 1931)
I guess there is a look at this operation hampered UI thread
You'd better change with Handler UI mechanism to inform about, so as not to hinder the UI, you can see the program in the end break point where the error
Reply:
cited a floor reply:
private Button camera_start, camera_pause; maybe you are not the find button next?
ah, they noted, is not the reason
Reply:
I have the same problem with the landlord, also reported this anomaly, but the strange thing is that I can run again android4.0 above the real machine, the fax machine in android2.3 quoted above was the exception, experts explain how matter .. ..
Reply:
parameters.setPreviewSize (screenWidth, screenHeight); / / Set the size of the preview pictures
---------------------
Here is the preview size requirements, these can not be directly used. Size can be used by parameters.flatten () to get the details!
Reply:
I also encountered the same problem
Reply:
Agreed to the fifth floor, the question arises in the low version.

No comments:

Post a Comment