OpenCV之處理圖像前的基本準備工作

        前面兩個部分分別介紹了在AS上安裝OpenCV的開發環境以及關於OpenCV的基本信息,本部分就來說一說使用OpenCV處理圖像的一些基本準備工作。

        我們知道在AS上開發Android的項目最後都會運行到模擬器或者移動終端上,我們這裏要使用OpenCV來處理圖片,那麼首先的獲取圖像,這裏可能主要分爲兩個大部分,一部分是圖片來源於本地,另一個是圖片來源於網絡。關於從網絡中獲取圖片這裏就先不介紹,主要說一下通過設備自帶的相機和本地文件來獲取圖像資源。這裏其實在我之前的博客就有介紹,有興趣的朋友可以轉至攝像頭調用與照片選擇

 

1,相機獲取圖片

打開相機:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = Uri.fromFile(new File(fielPath));
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);

獲取照片:

 Bitmap bitmap=BitmapFactory.decodeFile(filePath);

 

2,文件獲取圖片

打開文件選擇:

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "請選擇圖片"), FILE_REQUEST_CODE);

獲取圖片:

 private String handleImageOnKitKat(Intent data) {
        //獲取資源定位符
        Uri uri = data.getData();
        //選擇圖片路徑
        String imagePath = null;
        if (DocumentsContract.isDocumentUri(this, uri)) {
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                imagePath = getImagePath(contentUri, null);
            }

        } else if ("content".equalsIgnoreCase(uri.getScheme())) {

            imagePath = getImagePath(uri, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            imagePath = uri.getPath();
        }
        return imagePath;
    }




private String getImagePath(Uri externalContentUri, String selection) {
        String path = null;
        Cursor cursor = getContentResolver().query(externalContentUri, null, selection, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }

只需要將回調的Intent傳進到handleImageOnKitKat方法中即可獲取選擇的圖片路徑。

 

3,圖片壓縮與旋轉

壓縮:

Bitmap.createScaledBitmap(bitmap,imgWidth,imgHeight, true)                   

旋轉:

  /**
     *
     * @param bitmap
     * @return
     */
    public static Bitmap rotateBitmap(Bitmap bitmap,float rotateAngle){
        Matrix matrix  = new Matrix();
        matrix.setRotate(rotateAngle);
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight()
                        , matrix, true);

        return newBitmap;
    }

注意,在旋轉圖片的時候建議設置圖片Bitmap寬高一樣,否則在旋轉的過程中可能會報異常,因爲Bitmap就像一個容器,我們旋轉的是裏面的內容,外殼沒有變化,如果原來是長方形,旋轉之後外殼還是長方形,裏面的東西變成了躺着的長方形,也就是寬高對調了,你是肯定放不進去的,這個時候就會出現異常,但是如果是正方形就不會(前提是以90度爲單位旋轉)。

 

4,示例

4.1 示例效果

 

4.2 實現代碼

activity_main.xml代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.hfut.imagetoolsdemo.MainActivity">

    <ImageView
        android:id="@+id/image_show"
        android:layout_width="match_parent"
        android:layout_height="400sp" />

    <Button
        android:id="@+id/open_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="openCamera"
        android:text="打開相機" />

    <Button
        android:id="@+id/open_file"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="openFile"
        android:text="選擇文件" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="寬:" />

        <EditText
            android:id="@+id/bitmap_width"
            android:layout_width="150sp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="高:" />

        <EditText
            android:id="@+id/bitmap_height"
            android:layout_width="150sp"
            android:layout_height="wrap_content" />

    </LinearLayout>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="changeSize"
        android:text="調整大小" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="rotateImage"
        android:text="旋轉圖片" />
</LinearLayout>

 

MainActivity.java 代碼:

package com.hfut.imagetoolsdemo;

import android.Manifest;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author why
 * @date 2018-12-1 12:14:56
 */
public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    EditText imgWidth;
    EditText imgHeight;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image_show);
        imgWidth = findViewById(R.id.bitmap_width);
        imgHeight = findViewById(R.id.bitmap_height);
    }
    
    public void openCamera(View view) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri fileUri = Uri.fromFile(new File("mnt/sdcard/why.jpg"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, 1);
    }

    public void openFile(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "請選擇圖片"), 2);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                bitmap = BitmapFactory.decodeFile("mnt/sdcard/why.jpg");
                imageView.setImageBitmap(bitmap);
                break;
            case 2:
                String imagePath = handleImageOnKitKat(data);
                bitmap = BitmapFactory.decodeFile(imagePath);
                imageView.setImageBitmap(bitmap);
                break;
        }
    }


    public void changeSize(View view) {
        if (imgHeight.getText().toString().equals("") || imgWidth.getText().toString().equals("")) {
            Toast.makeText(this, "please enter the size info", Toast.LENGTH_SHORT).show();
        } else {
            bitmap = Bitmap.createScaledBitmap(bitmap,
                    Integer.parseInt(imgWidth.getText().toString()),
                    Integer.parseInt(imgHeight.getText().toString()), true);
            imageView.setImageBitmap(bitmap);
        }
    }

    public void rotateImage(View view) {
        bitmap = rotateBitmap(bitmap, 90);
        imageView.setImageBitmap(bitmap);
    }
    
    private String handleImageOnKitKat(Intent data) {
        //獲取資源定位符
        Uri uri = data.getData();
        //選擇圖片路徑
        String imagePath = null;
        if (DocumentsContract.isDocumentUri(this, uri)) {
            String docId = DocumentsContract.getDocumentId(uri);
            if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
                String id = docId.split(":")[1];
                String selection = MediaStore.Images.Media._ID + "=" + id;
                imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
            } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                imagePath = getImagePath(contentUri, null);
            }

        } else if ("content".equalsIgnoreCase(uri.getScheme())) {

            imagePath = getImagePath(uri, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            imagePath = uri.getPath();
        }
        return imagePath;
    }

    private String getImagePath(Uri externalContentUri, String selection) {
        String path = null;
        Cursor cursor = getContentResolver().query(externalContentUri, null, selection, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }

    private Bitmap rotateBitmap(Bitmap bitmap, float rotateAngle) {
        Matrix matrix = new Matrix();
        matrix.setRotate(rotateAngle);
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight()
                , matrix, true);

        return newBitmap;
    }

}

在配置文件中添加讀寫外部存儲和相機的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>

注意,因爲我是在5.1 的系統上測試的,在6.0及以上請添加權限申請部分代碼。到這裏,獲取本地圖片最基本的部分就介紹完了。更多類容請查看:

上一篇:關於OpenCV的主要信息基本介紹

下一篇:OpenCV中Mat與Android中Bitmap簡介

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章