在android中相机捕获图像后的全屏预览

时间:2010-07-31 09:33:22

标签: android android-emulator

我使用的是Android 1.5。 这是我的图像捕获代码:

public class CaptureImage extends Activity implements SurfaceHolder.Callback{
    private Camera camera=null;
    private SurfaceHolder surfaceHolder = null;
    private boolean previewRunning = false;
    private Context thisContext = this;
    private String imageName = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.image_capture);
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        Button btn =(Button) findViewById(R.id.capture_image_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                camera.takePicture(null, picCalBac, picCalBac);
            }
        });
    }

    Camera.PictureCallback picCalBac = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera) {
            if (paramArrayOfByte != null) {
                //Intent intent = new Intent();
                //imageName = "myImg.jpg";
                //storeByteImage(thisContext, paramArrayOfByte, 75, imageName);
                //setResult(RESULT_OK, intent);
                //finish();
            }
        }
    };

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        if (previewRunning) {
            camera.stopPreview();
            }
            Camera.Parameters p = camera.getParameters();
            p.setPreviewSize(arg2, arg3);
            p.setPictureSize(800, 600);
            camera.setParameters(p);
            try {
            camera.setPreviewDisplay(surfaceHolder);
            } catch (IOException e) {
                Log.d("IOException", e.getMessage());
                }
            camera.startPreview();
            previewRunning = true;
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        camera.stopPreview();
        previewRunning = false;
        camera.release();
    }

    private boolean storeByteImage(Context mContext, byte[] imageData,
            int quality, String expName) {

        FileOutputStream fileOutputStream = null;
        try {

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 1;

            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                    imageData.length,options);


            fileOutputStream = openFileOutput(imageName, Context.MODE_PRIVATE);


            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            Log.d("FileNotFoundException", e.getMessage());
        } catch (IOException e) {
            Log.d("IOException", e.getMessage());
        }
        return true;
    }

这是我的image_capture.xml:

<?xml version="1.0" encoding="utf-8"?>
<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_camera"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_weight="1">
    </SurfaceView>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:text="Capture" android:gravity="center_horizontal"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/capture_image_btn" />
    </LinearLayout>
</LinearLayout>

我在manifest.xml中添加了用户权限。

从另一个活动点击访问此代码。单击照片后,屏幕保持静止,点击快照。问题是相机窗口的高度和宽度约为40%,图像点击后显示的预览也较小,约为高度和宽度的80%。如何同时制作(相机窗口和点击后预览)全屏?

1 个答案:

答案 0 :(得分:2)

通过在onCreate

上添加以下代码解决了这个问题
surfaceHolder.setFixedSize(getWindow().getWindowManager()
                .getDefaultDisplay().getWidth(), getWindow().getWindowManager()
                .getDefaultDisplay().getHeight());