温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android关于图片的处理

发布时间:2020-07-30 12:33:00 来源:网络 阅读:6196 作者:雪飘七月 栏目:移动开发

一、布局中显示图片
在布局的xml中布局图片的时候用ImageView,用src去指定图片所在位置。如下代码所示,指定的就是工程目录(/res/drawable)中文件名为unknown.png的图片。
这里要注意的是Android Studio在布局时只认png格式的图片,即使是jpeg格式,仅把后缀改为png也不行,编译时会不通过。

<ImageView 
                android:id="@+id/iv_mytest"
                android:src="@drawable/unknown" />

但是,也不等于jpeg格式的图片就不能显示,我们可以通过如下代码处理的方式来展示到界面上。

String imgPath = Environment.getExternalStorageDirectory() + "test.jpg";
ImageView iv_mytest = (ImageView) findViewById(R.id.iv_mytest);
iv_mytest.setVisibility(View.VISIBLE);
if(!imgPath.equals("")) {
        Bitmap tempBitmap = BitmapFactory.decodeFile(imgPath);
        iv_mytest.setImageBitmap(tempBitmap);//显示图片
}

二、拍照后显示图片
拍照流程为获取缓存图片路径->进入拍照界面->拍照界面拍照后自动存到缓存图片路径中->进入回调函数->对缓存图片进行处理(如旋转缩放等)并存储到自己指定位置->删除缓存路径图片。
具体代码如下所示:

private String tmpfilename = "";
//调用拍照界面
private void photograph(){
        try {
                // 跳转至拍照界面
                String sdStatus = Environment.getExternalStorageState();
                if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
                        ToastUtil.showToastMsgError(MyTestActivity.this,"SD card is not avaiable/writeable right now.");
                        return;
                }

                tmpfilename=getTempFilePath();
                File out = new File(tmpfilename);

                Intent intentPhote = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                if (Build.VERSION.SDK_INT >= 24) {
                        //临时添加一个拍照权限
                        intentPhote.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        //通过FileProvider获取uri
                        contentUri = FileProvider.getUriForFile(getActivity(),
                                        "com.tmri.rfid.eri.internet.fileProvider",  out);
                        intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                } else {
                        mImageCaptureUri = Uri.fromFile(out);
                        intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                }

                startActivityForResult(intentPhote, 1);
        }catch (Exception e) {

        }
}

/**
 * 获取原缓存图片存储路径
 * @return
 */
private String getTempFilePath() {
        // 照片全路径
        String fileName = "";
        // 文件夹路径
        String pathUrl = Environment.getExternalStorageDirectory()+"/tmp/";

        imagename = "mytest.png";
        File file = new File(pathUrl);
        file.mkdirs();// 创建文件夹
        fileName = pathUrl + imagename;
        return fileName;
}

//拍取照后的回调
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true; // 设置了此属性一定要记得将值设置为false
                Bitmap bitmap = BitmapFactory.decodeFile(tmpfilename);
                // 防止OOM发生
                options.inJustDecodeBounds = false;
                saveMyBitmap(imagename,bitmap);

                File old = new File(Environment.getExternalStorageDirectory() + "/tmp/");
                FileUtil.deleteFile(old);//删除缓存图片

                String resultMsg = "图片已保存";
                ToastUtil.showToastMsgSuccess(this, resultMsg);
        }
}

//将图像保存到SD卡中
public void saveMyBitmap(String bitName, Bitmap mBitmap) {
        String thisDate = formatter_date.format(new Date());
        File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName);
        String realfilepath = f.getPath();
        FileOutputStream fOut = null;
        try {
                fOut = new FileOutputStream(f);
        } catch (Exception e) {
                e.printStackTrace();
        }

        Matrix matrix = new Matrix();

        // 按照固定大小对图片进行缩放
        matrix.postScale(0.3f, 0.3f);
        System.out.println(mBitmap.getWidth() + mBitmap.getHeight());
        if (mBitmap.getHeight() < mBitmap.getWidth()) {
                matrix.postRotate(90);  //翻转90度
        }
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

        try {
                fOut.write(("my secret message").getBytes());//我在这边偷偷给图片结尾藏了一些信息
                fOut.flush();
        } catch (IOException e) {
                e.printStackTrace();
        }
        try {
                fOut.close();
        } catch (IOException e) {
                e.printStackTrace();
        }
}

三、图片的处理
旋转、缩放等操作我们是通过Matrix来处理的,Matrix还有很多其他图形处理的方法,可以另开一篇去讲述。

Matrix matrix = new Matrix();
// 按照固定大小对图片进行缩放
matrix.postScale(0.3f, 0.3f);
if (mBitmap.getHeight() < mBitmap.getWidth()) {
        matrix.postRotate(90);  //翻转90度
}
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

上面是按比例缩小,下面是按固定分辨率缩放

Matrix matrix = new Matrix();
int dstWidth = 800;
int dstHeight = 600;
if (mBitmap.getHeight() > mBitmap.getWidth()) {
        matrix.postRotate(90);  //翻转90度

        final float sx = dstWidth / (float) mBitmap.getHeight();
        final float sy = dstHeight / (float) mBitmap.getWidth();
        matrix.postScale(sx, sy);
}else{
        final float sx = dstWidth / (float) mBitmap.getWidth();
        final float sy = dstHeight / (float) mBitmap.getHeight();
        matrix.postScale(sx, sy);
}
Bitmap endBit = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
endBit.compress(Bitmap.CompressFormat.JPEG, 100, fOut);//jpeg格式缩放的图片大小基本只占png的一半

保存图片,并将字符存入图片(有时候拍照后希望将一些信息与图片绑定起来,直接记录文件名又担心被人篡改,就想到了这种在图片文件末尾记录一些信息的方式)

String thisDate = formatter_date.format(new Date());
File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName);
String realfilepath = f.getPath();
FileOutputStream fOut = null;
try {
        fOut = new FileOutputStream(f);
} catch (Exception e) {
        e.printStackTrace();
}
//我在这边偷偷给图片结尾藏了一些信息
try {
        fOut.write(("my secret message").getBytes());
        fOut.flush();
} catch (IOException e) {
        e.printStackTrace();
}
try {
        fOut.close();
} catch (IOException e) {
        e.printStackTrace();
}

四、图片格式的差异
png、jpeg因为格式的差异,在内部添加字符信息时会不一样,比如png格式结尾处就是图片信息,所以添加的话直接在末尾添加就可以;而jpeg不行,jpeg末尾是有固定格式信息的,直接加载末尾虽然不影响图片显示,但是在解析时就会因为位置偏移解析出来的字符信息就不对了。
这一块内容还有待去深入研究下,当时也只是试验了两种格式,发现了这一问题。
更新于20190711,经尝试jpeg也可以再末尾写入信息,且jpeg缩放的图片大小仅占png格式缩放的图片一半大小。
但是,此时jpeg格式的图片直接解析是没有问题的,一经上传就无法解析末尾写入的信息了。
为何经过上传后末尾的信息无法解析的问题,还有待研究!还望知道的朋友不吝赐教!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI