1

我是初学者,我需要帮助。如何用相机拍照并保存或发送到下一个活动?

我尝试了几个选项,即带有图片回调的拍照和有意图的surfaceview/take。但是,两者都不能在 Android 2.3.3 上正常工作。有人可以找出下面我的代码的问题吗?

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sf_foto);
        mCamera = getCameraInstance();
        mCameraPreview = new SF_CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.takePicture(myShutterCallback, mPicture_RAW, mPicture);
            }
        });
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open(0);
            camera.setDisplayOrientation(90);
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    ShutterCallback myShutterCallback = new ShutterCallback(){
         @Override
         public void onShutter() {}
    };

    PictureCallback mPicture_RAW = new PictureCallback(){
         @Override
         public void onPictureTaken(byte[] arg0, Camera arg1) {}
    };

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 
            Intent i = new Intent(StyloveFoto.this, Filter.class);
            startActivity(i);
        }
    };

    protected File getOutputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"KWAlbum");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("KWAlbum", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "KW" + ".jpg");

        return mediaFile;
    }

我的表面观点:

public class SF_CameraPreview extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mSurfaceHolder;
    private Camera mCamera;

    public SF_CameraPreview(Context context, Camera camera) {
        super(context);
        this.mCamera = camera;
        this.mSurfaceHolder = this.getHolder();
        this.mSurfaceHolder.addCallback(this);
        this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();
        } catch (IOException e) {
            // left blank for now
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        //mCamera.stopPreview();
        mCamera.release();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
        int width, int height) {
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.set("orientation", "portrait");
            mCamera.setParameters(parameters);
            mCamera.startPreview();
        } catch (Exception e) {
            // intentionally left blank for a test
        }
    }
}
4

3 回答 3

0

而不是这一行: fos.write(data);

write : fos.write(data,0,data.length);

如果您想将其传递给下一个活动:

 Intent i = new Intent(StyloveFoto.this, Filter.class);
 i.putExtra("myImage",data);
 startActivity(i);

然后在 oncreate 方法中的类过滤器中

byte[] myImage = getIntent()getIntent().getByteArrayExtra("myImage");
于 2013-06-23T15:09:48.867 回答
0

我遇到了同样的问题,我之前只是解决了它。问题是它mPicture是一个对象PictureCallback。您不能将意图设置为指向Filter.classfrom StyloveFoto.this,因为它在PictureCallback界面中。试试这个:

Intent i = new Intent(getBaseContext() , Filter.class);
startActivity(i);

java中的一个大陷阱....希望它有所帮助:)

于 2014-02-15T15:32:38.647 回答
0

您可以像这样保存图片并将路径传递给另一个活动

final File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + "_pic.jpg");
        OutputStream output = null;
        try {
            output = new FileOutputStream(file);
            output.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

或从相机活动中传递数据:

Intent intent = new Intent(getApplicationContext(), your_class.class);
    intent.putExtra("path", path);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

接收活动

byte[] data = getIntent().getByteArrayExtra("path");
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
image.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200));
于 2018-09-24T07:30:53.393 回答