Android's processing of pictures

1. Display the picture
in the layout When laying out the picture in the xml of the layout, use ImageView, and use src to specify the location of the picture. As shown in the code below, the specified image is the file named unknown.png in the project directory (/res/drawable).
It should be noted here that Android Studio only recognizes images in png format during layout . Even if it is in jpeg format, it is not enough to just change the suffix to png, and the compilation will fail.

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

However, it does not mean that pictures in jpeg format cannot be displayed. We can display them on the interface through the following code processing.

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);//显示图片
}

2. Display the picture after taking the picture The picture-
taking process is to obtain the cached picture path -> enter the camera interface -> automatically save the picture to the cached picture path after taking the picture -> enter the callback function -> process the cached picture (such as rotation and zoom, etc.) and Store to your designated location -> delete the cached path image.
The specific code is as follows:

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);
                intentPhote.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                Uri uri = Uri.fromFile(out);
                // 获取拍照后未压缩的原图片,并保存在uri路径中
                intentPhote.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                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();
        }
}

3. Image processing
, rotation, scaling and other operations are handled by Matrix. Matrix has many other graphics processing methods, which can be described in a separate article.

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);

Save the picture and save the characters into the picture (sometimes after taking a picture, you want to bind some information to the picture, record the file name directly and worry about being tampered with, so I thought of this way of recording some information at the end of the picture file)

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();
}

4. Differences in image formats
png and jpeg will be different when adding character information internally due to the difference in format. For example, the end of png format is image information, so if you add it, you can add it directly at the end; but jpeg does not work, and the end of jpeg is If there is fixed format information, although the direct loading at the end does not affect the display of the picture, the character information parsed due to the position offset will be wrong during parsing.
This piece of content still needs to be studied in depth. At that time, only two formats were tested and this problem was found.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325947134&siteId=291194637