0
    package map.demo;
 public class MapDemoActivity extends Activity {

        Button capture;
        ImageView image;
        int cameracode=100;
        Bitmap bm;
        Boolean result;
        FileOutputStream fos;
        File sd;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            capture=(Button)findViewById(R.id.capture);
            capture.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    image=(ImageView)findViewById(R.id.image);
                    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, cameracode);  
                }
            });

        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub



                        if(requestCode==100)
                        {



                            bm=(Bitmap) data.getExtras().get("data");

                            image.setImageBitmap(bm);


                            image.setDrawingCacheEnabled(true);
                            bm = image.getDrawingCache();

                                if(bm==null)
                                {
                                    Toast.makeText(getApplicationContext(), "Image is null", 1000).show();
                                }
                                else
                                {
                                    try {

                                        fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg"));

                                        result=bm.compress(CompressFormat.JPEG, 75, fos);

                                        fos.flush();
                                        fos.close();

                                    } catch (FileNotFoundException e) {
                                        // TODO Auto-generated catch block

                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block

                                        e.printStackTrace();
                                    }

                            }       
                        }

            super.onActivityResult(requestCode, resultCode, data);
        }
    }

カメラから画像をキャプチャし、画像を画像ビューに設定し、画像をjpegに変換するために上記のコードを実行していますが、画像が取得されず、nullが表示されます。つまり、画像キャプチャ後のコード bm=null です。しかし、画像ビューにはデフォルトでカメラ用の画像が表示されます(エミュレータを使用して画像をキャプチャしています)。

4

3 に答える 3

0

こんにちは私は何かが足りないかもしれませんが、これと同じようにカメラを起動するためのコードはありますか?

public CameraView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // We're implementing the Callback interface and want to get notified
    // about certain surface events.
    getHolder().addCallback( this );
    // We're changing the surface to a PUSH surface, meaning we're receiving
    // all buffer data from another component - the camera, in this case.
    getHolder().setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
}

public void surfaceCreated( SurfaceHolder holder ) {
    // Once the surface is created, simply open a handle to the camera hardware.
    //camera = Camera.open();
    try {
        camera = Camera.open();
        camera.setPreviewDisplay( holder );
    } catch( IOException e ) {
        e.printStackTrace();
    }
}

public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ) {
    // This method is called when the surface changes, e.g. when it's size is set.
    // We use the opportunity to initialize the camera preview display dimensions.
    Log.d(TAG, "surfaceChanged: " + width + "x" + height);
    Camera.Parameters p = camera.getParameters();
    //p.set("orientation", "portrait");
    int w = p.getPreviewSize().width;
    int h = p.getPreviewSize().height;
    p.setPreviewSize( w, h );       
    camera.setParameters( p );

    // ...and start previewing. From now on, the camera keeps pushing preview
    // images to the surface.
    camera.startPreview();
}

また、マニフェストで次のように権限を設定したと仮定します。

 <uses-permission android:name="android.permission.CAMERA"/>

また、カメラが適切に設定されている場合にのみ、エミュレータがチェッカーボードパターンを表示することに注意してください。アスペクト比が正しく設定されている場合、パターンは正方形である必要があります(長方形は画像が歪んでいることを意味します)。

于 2012-06-28T11:44:51.453 に答える
0

ここで私が考える問題は、ImageViewに完全に描画する時間を提供しておらず、代わりに、キャッシュが存在する前であってもキャッシュを取得しようとしていることです。したがって、常にnullになり、Toastが表示されます。代わりに、ここにメソッドがあります。これをチェックしてください、

この行を削除して、どうなるかを確認してください。

 bm = image.getDrawingCache();

その理由は、キャプチャされた画像をこのビットマップオブジェクトにすでに保存し、ImageViewに設定しているためです。では、なぜもう一度ImageViewを使用してビットマップオブジェクトを取得するのでしょうか。画像のサイズを縮小したい場合は、ビットマップクラスのcreateScaledBitampメソッドを使用できます。

于 2012-06-28T11:45:34.413 に答える