Maybe this piece of code won't work, but i'm writing an app that successfully uses the camera. No crashes :) ... ... so far ... :=)
On Feb 17, 1:35 am, cindy <ypu01...@yahoo.com> wrote: > I tried google's camera API sample code. I found there are 2 problems: > 1> It only can take picture in Landscape orientation > 2> After click the space "key", the application crashes. WOw:) > > XML: > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:layout_width="fill_parent" > android:layout_height="fill_parent" > android:orientation="vertical"> > <SurfaceView android:id="@+id/surface" > android:layout_width="fill_parent" > android:layout_height="10dip" > android:layout_weight="1"> > </SurfaceView> > </LinearLayout> > Java code: > /** > * Copyright (c) 2007, Google Inc. > * > * Licensed under the Apache License, Version 2.0 (the "License"); > * you may not use this file except in compliance with the License. > * You may obtain a copy of the License at > * > * http://www.apache.org/licenses/LICENSE-2.0 > * > * Unless required by applicable law or agreed to in writing, > software > * distributed under the License is distributed on an "AS IS" BASIS, > * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. > * See the License for the specific language governing permissions > and > * limitations under the License. > */ > > package com.android.cameraapitest; > > import android.app.Activity; > import android.content.Intent; > import android.graphics.Canvas; > import android.graphics.Paint; > import android.graphics.PixelFormat; > import android.graphics.Rect; > import android.net.Uri; > import android.os.Handler; > import android.os.Message; > import android.os.Bundle; > import android.provider.MediaStore.Images; > import android.provider.MediaStore.Video; > import android.view.Menu; > import android.view.MenuItem; > import android.view.SurfaceHolder; > import android.view.SurfaceView; > import android.view.KeyEvent; > import android.hardware.Camera; > > import android.util.Log; > > public class CameraApiTest extends Activity implements > SurfaceHolder.Callback > { > private static final String TAG = "CameraApiTest"; > Camera mCamera; > boolean mPreviewRunning = false; > > public void onCreate(Bundle icicle) > { > super.onCreate(icicle); > > Log.e(TAG, "onCreate"); > > getWindow().setFormat(PixelFormat.TRANSLUCENT); > > setContentView(R.layout.camera_api_test); > mSurfaceView = (SurfaceView)findViewById(R.id.surface); > > mSurfaceHolder = mSurfaceView.getHolder(); > mSurfaceHolder.addCallback(this); > mSurfaceHolder.setType > (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); > } > > public boolean onCreateOptionsMenu(android.view.Menu menu) { > MenuItem item = menu.add(0, 0, 0, "goto gallery"); > item.setOnMenuItemClickListener(new > MenuItem.OnMenuItemClickListener() { > public boolean onMenuItemClick(MenuItem item) { > Uri target = Uri.parse("content://media/external/ > images/media"); > Intent intent = new Intent(Intent.ACTION_VIEW, > target); > startActivity(intent); > return true; > } > }); > return true; > } > > @Override > protected void onRestoreInstanceState(Bundle savedInstanceState) > { > super.onRestoreInstanceState(savedInstanceState); > } > > Camera.PictureCallback mPictureCallback = new > Camera.PictureCallback() { > public void onPictureTaken(byte[] data, Camera c) { > Log.e(TAG, "PICTURE CALLBACK: data.length = " + > data.length); > mCamera.startPreview(); > } > }; > > public boolean onKeyDown(int keyCode, KeyEvent event) > { > if (keyCode == KeyEvent.KEYCODE_BACK) { > return super.onKeyDown(keyCode, event); > } > > if (keyCode == KeyEvent.KEYCODE_SPACE) { > mCamera.takePicture(null, mPictureCallback); > return true; > } > > return false; > } > > protected void onResume() > { > Log.e(TAG, "onResume"); > super.onResume(); > } > > protected void onSaveInstanceState(Bundle outState) > { > super.onSaveInstanceState(outState); > } > > protected void onStop() > { > Log.e(TAG, "onStop"); > super.onStop(); > } > > public void surfaceCreated(SurfaceHolder holder) > { > Log.e(TAG, "surfaceCreated"); > mCamera = Camera.open(); > //mCamera.startPreview(); > } > > public void surfaceChanged(SurfaceHolder holder, int format, int > w, int h) > { > Log.e(TAG, "surfaceChanged"); > > // XXX stopPreview() will crash if preview is not running > if (mPreviewRunning) { > mCamera.stopPreview(); > } > > Camera.Parameters p = mCamera.getParameters(); > p.setPreviewSize(w, h); > mCamera.setParameters(p); > mCamera.setPreviewDisplay(holder); > mCamera.startPreview(); > mPreviewRunning = true; > } > > public void surfaceDestroyed(SurfaceHolder holder) > { > Log.e(TAG, "surfaceDestroyed"); > mCamera.stopPreview(); > mPreviewRunning = false; > mCamera.release(); > } > > private SurfaceView mSurfaceView; > private SurfaceHolder mSurfaceHolder; > > > > }- Hide quoted text - > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---