java-Android day06

Android Day03

by 木石溪

border="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=452857140&auto=1&height=66">

今天被实验室大佬抓住,说我在不干正事,让写个灰度直方图均衡化的摄像Demo,
怎么可能屈服在他淫威下,我写还不行。。。怂~~


修订:

	2019/01/16
	* 修改了加载图片为略缩图;
	* 增加了内存读取存储授权;

实现思路:

  • Part1:调用摄像头
    1. 动态授权;
    2. 设置拍照意图,启动拍照;
  • Part2:图片处理
    3. 进行图像二值化;
    4. 进行灰度直方图均衡化;
    5. 显示处理图片;

step1:界面(按键 + 均衡化显示)

主要部件:

  • 拍照 Button
  • 图片显示 ImageView
    <?xml version="1.0" encoding="utf-8"?>
   <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:ignore="TableFor"
            tools:context=".MainActivity">
    
        <Button android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/bt_camera"
                android:text="@string/bt_camera"
                app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="@+id/imageView"
                android:layout_marginBottom="3dp" app:layout_constraintBottom_toTopOf="@+id/imageView"/>
        <ImageView android:layout_width="0dp" android:layout_height="0dp"
                   android:id="@+id/imageView" app:layout_constraintTop_toBottomOf="@+id/bt_camera"
                   android:layout_marginBottom="2dp" app:layout_constraintEnd_toEndOf="parent"
                   app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>

Step2:实现摄像头动态授权和调用

Part01 : 设置按键监听
	// load layout and find view parts by id
	setContentView(R.layout.activity_main);
	Button bt_camera = findViewById(R.id.bt_camera);
	mMImageView = findViewById(R.id.imageView);

	// set listener of button clicked
	bt_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSION_REQUEST_CODE);
                } else {
                    takePhoto();
                }
            }
        });

Part02: 进行M 运行权限授权

	@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSION_REQUEST_CODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    takePhoto();
                } else {
                    Toast.makeText(this, "THE PERMISSION DENIED", Toast.LENGTH_SHORT).show();
                }
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

Part03:设置拍照程序

	private void takePhoto() {
		Intent photo_act = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(photo_act, TAKE_PHOTO_CODE);
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case TAKE_PHOTO_CODE:
                    Bundle extras = data.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
            		
            		// digital image processing
            		Bitmap dst = equalize(imageBitmap);
            		mImageView.setImageBitmap(dst);
                    break;
                default:
                    break;
            }
        }
   }

Part04: 图像二值化和直方图均衡化处理

	//--------------------------------------
   // Digital Image Processing
   //--------------------------------------
   private Bitmap equalize(Bitmap src){
   
       // 1. convert Bitmap to color array
       int rows = src.getHeight();
       int cols = src.getWidth();
       int size = rows * cols;
       int[] img = new int[rows * cols];
       
       // each element of color array is a PACKED color code for that pixel, which can be decomposed to A,R,G and B
       src.getPixels(img,0,cols,0,0,cols,rows);
       
       // 2. convert ARGB/RGB color code 2 Gray Scale and calculate image histogram
       // since we've already allocated a int array for all the pixels, there's no need to allocated another 'uchar' for them
       int base,id,color,level,r,c = 0;
       int[] hist = new int[256];
       for(r=0; r<rows; r++){
           base = r*cols;
           for(c=0; c<cols; c++){
               id = base + c;
               color = img[id];
               level = (int)Math.round((0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)));
               img[id] = level;
               // calculate histogram at the same time
               hist[level]++;
           }
       }
       
       // 3. get PDF
       double[] pdf = new double[256];
       for(int i=0; i<256; i++){
           pdf[i] = (double)hist[i]/size;
       }
       
       // 4. get CDF and generate dictionary
       int[] dict = new int[256];
       double tmpd = 0;
       for(int m=0; m<256; m++){
           tmpd = 0;
           for(int n=0; n<=m; n++){
               tmpd += pdf[n];
           }
           dict[m] = (int)Math.round(255*tmpd);
       }
       
       // 5. map img back to color code using dictionary
       for(r=0; r<rows; r++){
           base = r*cols;
           for(c=0; c<cols; c++){
               id = base + c;
               level = dict[img[id]];
               img[id] = Color.rgb(level,level,level);
           }
       }
       System.out.println(dict[200] + ", " + dict[201] + ", " + dict[202]);
       System.out.println(img[200] + ", " + img[201] + ", " + img[202]);

       // 6. set Bitmap
       return Bitmap.createBitmap(img,cols,rows,src.getConfig());
   }

结果:
APP_RUNNING_RESULT

未解决问题

  1. 目前处理图片为略缩图,图像清晰度不够;
  2. 灰度直方图均衡化算法有待提升;

修订Part 1:授权部分

发现原图存储需要读取内存的权限于是修改了权限的赋予;

 bt_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= 23) {
                    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
                    //验证是否许可权限
                    //for (String str : permissions) {
                        if (MainActivity.this.checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED ||
                            MainActivity.this.checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED ||
                            MainActivity.this.checkSelfPermission(permissions[2]) != PackageManager.PERMISSION_GRANTED) {
                            //申请权限
                            MainActivity.this.requestPermissions(permissions, PERMISSION_REQUEST_CODE_STORAGE_WRITE);
                            MainActivity.this.requestPermissions(permissions, PERMISSION_REQUEST_CODE_STORAGE_READ);
                            MainActivity.this.requestPermissions(permissions, PERMISSION_REQUEST_CODE_CAMERA);
                        } else {
                            takePhoto();
                        //}
                    }
                }
            }
        });

修订Part 2:拍照部分

由于上文拍照方式只能得到略缩图,于是想到一种得到原图方式,先存,在传,利用数据流

private void takePhoto() {

        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(path);
            if (!file.exists()) {
                file.mkdir();
            }
        }
        String fileName = "test.jpg";
        Intent photo_act = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        picPath = path + fileName;
        Uri uri = Uri.fromFile(new File(picPath));
        photo_act.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(photo_act, TAKE_PHOTO_CODE);
    }

修订Part 3:响应处理部分

                    try {
                        fis = new FileInputStream(picPath);
                        Bitmap src = BitmapFactory.decodeStream(fis);
                        Bitmap dst = equalize(src);
                        mMImageView_dst.setImageBitmap(dst);
                        //mMImageView_dst.setImageBitmap(src);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (fis != null) {
                                fis.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值