Android 摄像机图像转换

Android 摄像机图像转换,android,Android,我正在做上面的代码,从相机中捕获图像,并将图像设置为图像视图 &将图像转换为jpeg, 但我没有得到图像,它显示为空。i、 图像捕获后,我的代码中的e bm=null。 但是图像视图显示图像,这是默认情况下用于照相机的图像,我正在使用emulator捕获图像。您没有获得图像,因为您没有提供任何图像URI来保存图像 请参阅链接。您好,我可能遗漏了一些内容,但您是否有启动摄像头模拟程序的代码: package map.demo; public class MapDemoActivity e

我正在做上面的代码,从相机中捕获图像,并将图像设置为图像视图 &将图像转换为jpeg, 但我没有得到图像,它显示为空。i、 图像捕获后,我的代码中的e bm=null。
但是图像视图显示图像,这是默认情况下用于照相机的图像,我正在使用emulator捕获图像。

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


请参阅链接。

您好,我可能遗漏了一些内容,但您是否有启动摄像头模拟程序的代码:

    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);
        }
    }
我假设您也在清单中设置了如下权限:

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();
}

另外请注意,如果相机设置正确,模拟器将仅显示棋盘格图案。如果纵横比设置正确,则图案必须为正方形矩形表示图像失真。

我认为这里的问题是,您没有为ImageView提供完全绘制的时间,而是在缓存出现之前尝试获取缓存。所以你总是得到空值,你的吐司会显示出来。相反,这里有一个方法。看看这个

拆下这条线,看看它是怎么走的

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

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

很高兴听到。。那么,接受这个作为答案怎么样
 bm = image.getDrawingCache();