Android相机照片保存方向错误

时间:2016-09-19 10:10:05

标签: android camera orientation photo

我正在尝试拍照并让照片以纵向方式保存。即使我改变了清单也没有意图的方向来设置它的肖像,我也做不到。我谷歌并尝试了各种方式,但都没有。请协助。

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("request", requestCode + "  result  " + resultCode + "  intent  "
                + data);
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_SAVE_ITEM){
            clearItemContents();
        }

        else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
        {ExifInterface ei = new ExifInterface(cameraImagePath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotateImage(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotateImage(bitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotateImage(bitmap, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                break;
        }


            openGallery();
        }






        else{
            if (gallery == true) {
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                        && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    imageArray = imageToByteArray(new File(picturePath));

                    setImagePicture(picturePath);
                } else {

                }
            } else if (gallery == false) {
                File file = new File(cameraImagePath);
                if (file.exists()) {
                    imageArray = imageToByteArray(file);
                    setImagePicture(cameraImagePath);
                } else {

                }

            } else {
                this.finish();
                startActivity(new Intent(this, DefectAdd.class));
            }}
    }
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels)
    {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }


    private byte[] imageToByteArray(File file) {
        try {
            FileInputStream fis = new FileInputStream(file);


            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];

            for (int readNum; (readNum = fis.read(buf)) != -1;) {

                bos.write(buf, 0, readNum);
                System.out.println("read " + readNum + " bytes,");
            }
            byte[] bytes = bos.toByteArray();
            return bytes;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private void setImagePicture(String photoPath) {
        editTextItemPic.setVisibility(ImageView.GONE);
        itemImage.setVisibility(ImageView.VISIBLE);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
        itemImage.setRotation(90);
        itemImage.setImageBitmap(bitmap);
    }





    public void showDialog() {

        builder.setTitle("Choose Action!");
        builder.setItems(dialogItems, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                if (item == 0) {
                    openGallery();
                } else if(item == 1) {
                    openCamera();


                }else{
                    imageArray = null;
                    editTextItemPic.setVisibility(ImageView.VISIBLE);
                    itemImage.setVisibility(ImageView.GONE);
                }
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void openGallery() {
        MaterialAddActivity.gallery = true;
        Intent i = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, MaterialAddActivity.RESULT_LOAD_IMAGE);
    }

    public void openCamera() {
        USE_CAMERA=1;

        Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        cameraIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

    }

    private String appFolderCheckandCreate() {

        String appFolderPath = "";
        File externalStorage =Environment.getExternalStorageDirectory();


        if (externalStorage.canWrite()) {
            appFolderPath = "/storage/emulated/0/DCIM/Camera/";
            File dir = new File(externalStorage+"/storage/emulated/0/DCIM/Camera/");


            if (!dir.exists()) {
                dir.mkdirs();
            }

        } else {

        }

        return appFolderPath;
    }

    @SuppressLint("SimpleDateFormat") private String getTimeStamp() {

        final long timestamp = new Date().getTime();

        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timestamp);

        final String timeString = new SimpleDateFormat("HH_mm_ss_SSS")
                .format(cal.getTime());

        return timeString;
    }

    public void saveImage()
    {
        File filename;
        try
        {
            USE_CAMERA=0;
            String path = Environment.getExternalStorageDirectory().toString();

            new File(path + "/QSHelper").mkdirs();
            filename = new File(path + "/QSHelper/image"+LASTINSERTEDID+".png");

            FileOutputStream out = new FileOutputStream(filename);

            photo.compress(Bitmap.CompressFormat.PNG, 100, out);

            out.flush();
            out.close();

            MediaStore.Images.Media.insertImage(getContentResolver(),
                    filename.getAbsolutePath(), filename.getName(),
                    filename.getName());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

0 个答案:

没有答案