以编程方式从android拍照

时间:2014-06-02 12:27:26

标签: android image capture

我正在创建一个照片编辑应用程序,并尝试从Android相机捕获照片。我使用以下代码onPictureTaken:

@Override
    public void onPictureTaken(byte[] data, Camera camera) {

        BitmapFactory.Options opt;

        opt = new BitmapFactory.Options();
        opt.inTempStorage = new byte[16 * 1024];
        Parameters parameters = camera.getParameters();
        Size size = parameters.getPictureSize();
        int height11 = size.height;
        int width11 = size.width;
        float mb = (width11 * height11) / 1024000;
        if (mb > 4f)
            opt.inSampleSize = 4;
        else if (mb > 3f)
            opt.inSampleSize = 2;
        Bitmap bitmapPicture = null;
        bitmapPicture = BitmapFactory.decodeByteArray(data, 0, data.length,opt);
        bitmapPicture=Bitmap.createScaledBitmap(bitmapPicture,(int)CaptureImage.screen_width,(int)CaptureImage.screen_height,false);
        if(((CaptureImage)context).cameraType==1){

                    Matrix matrix = new Matrix();
                    if(((CaptureImage)context).screenType==0)
                    matrix.postRotate(180);
                    bitmapPicture = Bitmap.createBitmap(bitmapPicture, 0, 0,
                            bitmapPicture.getWidth(), bitmapPicture.getHeight(), matrix,
                            true);
        }
        // write captured image in sd card
        new File(Constants.IMAGES_PATH).mkdirs();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        photoFile = "Picture_" + date + ".jpg";
        File pictureFile = new File(Constants.IMAGES_PATH, photoFile);
        imgUri = Uri.fromFile(pictureFile);
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.write(data);
            fos.close();
            ExifInterface exif = new ExifInterface(pictureFile.getAbsolutePath());
            if(((CaptureImage)context).screenType==0){
                        if(((CaptureImage)context).cameraType==0)
                            exif.setAttribute(ExifInterface.TAG_ORIENTATION,""+ExifInterface.ORIENTATION_ROTATE_90);
                        if(((CaptureImage)context).cameraType==1)
                            exif.setAttribute(ExifInterface.TAG_ORIENTATION,""+ExifInterface.ORIENTATION_ROTATE_90);
             }
            else{
                        //if(((CaptureImage)context).cameraType==0)
                    //      exif.setAttribute(ExifInterface.TAG_ORIENTATION,""+ExifInterface.ORIENTATION_ROTATE_270);
    //                  if(((CaptureImage)context).cameraType==1)
                            //exif.setAttribute(ExifInterface.TAG_ORIENTATION,""+ExifInterface.ORIENTATION_ROTATE_180);
            }


            exif.saveAttributes();
        } catch (IOException exception) {
            Log.v("Error", exception.getMessage());
        }

        Intent intent = new Intent(context, Home.class);
        intent.putExtra(Constants.CAPTURED_IMAGE,imgUri);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

以上代码适用于所有手机,但在使用此代码时,nexus 4上的代码图像未保存,并显示空白屏幕。

0 个答案:

没有答案