发送到服务器时图像旋转到90度

时间:2017-02-22 02:59:48

标签: android

我实现了一个捕获图像并将其发送到服务器的函数。

如果我以纵向模式拍照然后将其发送到服务器,则图像始终向左旋转90度 但是,如果我用水平模式重复这一点,一切正常。

所以,我想出了一个主意 我将图片转换为位图对象并获得宽度和高度。 我厌倦了做的是在发送到服务器之前将图片旋转到90度(当我尝试使用肖像模式时) 然而,它从来没有奏效(带有纵向模式的图片在宽度上也有更多的像素......)

任何人都可以给我一些提示吗?

private void call_camera(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, PICK_FROM_CAMERA);
}

这是用于调用相机功能。

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode != RESULT_OK)
        return;

    if(requestCode == PICK_FROM_CAMERA){

        imageUri = data.getData();
        Log.d("메시지", "uri = "+imageUri);


        Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
        c.moveToNext();
        absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
    }
}

我使用absolutePath创建了一个File对象 然后将其发送到服务器。

fileInputStream = new FileInputStream(file);
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0) {

        DataOutputStream dataWrite = new     DataOutputStream(con.getOutputStream());
            dataWrite.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        fileInputStream.close();

        wr.writeBytes("\r\n--" + boundary + "--\r\n");
        wr.flush();

1 个答案:

答案 0 :(得分:1)

尝试以下方法

private Bitmap fixOrientation(Bitmap bitmap) {
    ExifInterface ei = null;
    Bitmap selectedBitmap;
    try {
        ei = new ExifInterface(mCurrentPhotoPath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            selectedBitmap = rotateImage(bitmap, 90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            selectedBitmap = rotateImage(bitmap, 180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            selectedBitmap = rotateImage(bitmap, 270);
            break;
        case ExifInterface.ORIENTATION_NORMAL:
            selectedBitmap = bitmap;
            break;
        default:
            selectedBitmap = bitmap;
    }
return selectedBitmap;
}
相关问题