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,但我没有得到图像,它显示为空。即在图像捕获后我的代码 bm=null 中。但图像视图显示默认情况下用于相机的图像(我正在使用模拟器捕获图像)。

4

3 回答 3

0

我认为这里的问题是,您没有为 ImageView 提供完全绘制的时间,而是您尝试在缓存存在之前获取缓存。所以你总是会得到 null 并且你的 Toast 会被显示。取而代之的是一种方法。看一下这个,

删除这条线,看看它是怎么回事。

 bm = image.getDrawingCache();

原因是您已经将捕获的图像保存到此 Bitmap 对象并将其设置为 ImageView。那么为什么又要依赖ImageView来获取位图对象呢。如果要减小图像的大小,可以使用Bitmap 类中的createScaledBitamp方法。

于 2012-06-28T11:45:34.413 回答
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

您没有获得图像,因为您没有提供任何将保存图像的图像 URI。

请看这个链接。

于 2012-06-28T11:42:15.257 回答