1、MainActivity.java函数代码
package com.example.camerapictureoperation;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Uri mImageCaptureUri;
private ImageView mImageView;
EditText txinfo;
Bitmap bitmap;
Button bn1,bn2,bn3,bn4;
String path= Environment.getExternalStorageDirectory()+ File.separator+"DCIM"+File.separator+"Camera"+File.separator+"temp.jpg";
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txinfo =findViewById(R.id.tx);
mImageView = findViewById(R.id.imageView);
bn1 =findViewById(R.id.bn1);
bn2 =findViewById(R.id.bn2);
bn3 =findViewById(R.id.bn3);
bn4 =findViewById(R.id.bn4);
Permission();
OpenCamera(mImageView);
SelectPictureFromSDCard(mImageView);
SavePicture(mImageView);
DrawonPicture(mImageView);
}
//照片打开函数:定义按钮1响应函数,实现手机摄像头的调用,之后由下面的回调函数实现所拍摄的照片显示至图形控件区域
public void OpenCamera(View v) {
bn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri=Uri.fromFile(new File(path));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(cameraIntent, 123); }
});
}
//相册打开函数:定义按钮2响应函数,实现从手机相册浏览、选择图片,之后由下面的回调函数实现图片显示至图形控件区域
public void SelectPictureFromSDCard(View v) {
bn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), 456);
}
});
}
//存储函数:定义按钮3响应函数,获取图片及图片名称,调用图片存储函数,实现图片按给定名称存储至手机内存
public void SavePicture(View v) {
bn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String picturename =txinfo.getText().toString();
Bitmap bitm=Bitmap.createBitmap(bitmap);
try {
saveToLocal(bitm,picturename);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
//图片涂鸦函数:定义按钮4响应函数,实现图形控件区域的图像的各项参数调用及图片涂鸦操作
public void DrawonPicture(View v) {
bn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bms=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth()-1,bitmap.getHeight()-1);
//画红色矩形框,模拟扫描光线
int intx=bms.getWidth()-1;
int inty=bms.getHeight()-1;
RectF rect;
rect = new RectF(0,inty/2 -3,intx,inty/2 +3);
Canvas canvas = new Canvas(bms);
Paint paint;
paint =new Paint();
paint.setColor(Color.RED );
canvas.drawRect(rect, paint);
mImageView.setImageBitmap(bms);
//从左向右取出各像素值,计算平均像素
int pix=0;
int avepix=0;
for (int i=1;i<intx;i++){
pix=bms.getPixel(i,inty/2-5) ;
avepix=avepix+pix/intx;
}
txinfo.setText("平均像素值为"+avepix);
//从左至由读取条码图片各像素值,对比平均像素值,将像素2值化
/*String str="";
for (int i=1;i<intx;i++){
pix=bms.getPixel(i,inty/2-5) ;
if (pix<avepix) str=str+"1";
else if (pix>avepix) str=str+"0";
}*/
}
});
}
//图片存储函数:用来实现将图片按给定名称存储至手机内存
public void saveToLocal(Bitmap bitmap, String bitName) throws IOException {
File file = new File("/sdcard/DCIM/Camera/" + bitName + ".jpg");
if (file.exists()) {
file.delete();
}
FileOutputStream out;
try {
out = new FileOutputStream(file);
if (bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)) {
out.flush();
out.close();
//保存图片后发送广播通知更新数据库
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
this.sendBroadcast(intent);
txinfo.setText("恭喜!图片存储成功!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//回调函数:用来处理拍照函数及打开图片函数运行的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
if (requestCode == 456) {
mImageCaptureUri = data.getData();
mImageView.setImageURI(mImageCaptureUri);
mImageView.setDrawingCacheEnabled(true);
bitmap= Bitmap.createBitmap(mImageView.getDrawingCache());
mImageView.setDrawingCacheEnabled(false);
}
else if (requestCode == 123) {
mImageView.setImageURI(mImageCaptureUri);
mImageView.setDrawingCacheEnabled(true);
bitmap= Bitmap.createBitmap(mImageView.getDrawingCache());
mImageView.setDrawingCacheEnabled(false);
}
mImageView.setImageBitmap(bitmap);
}
//授权函数,须在主程序中调用一下
public void Permission(){
//安卓7.0调用相机
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
//动态申请权限
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,//联系人的权限
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};//读写SD卡权限
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
}
}
}
}
}
2、activity_main.xml函数代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="-78dp"
tools:layout_editor_absoluteY="-251dp">
<Button
android:id="@+id/bn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="OpenCamera"
android:text="Button" />
<Button
android:id="@+id/bn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="SelectPictureFromSDCard"
android:text="Button" />
<Button
android:id="@+id/bn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="SavePicture"
android:text="Button" />
<Button
android:id="@+id/bn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="DrawonPicture"
android:text="Button" />
<EditText
android:id="@+id/tx"
android:layout_width="match_parent"
android:layout_height="99dp"
android:text="EditText" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3、AndroidManifest.xml函数代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camerapictureoperation">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraPictureOperation">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>