Android ----- achieve a picture Add Fonts

Add to realize picture fonts, picture rotation function: xml layout file follows a simple layout ImageView

<com.example.hsjgapp.RotateImageView        //这里存放要展示的图片
    android:id="@+id/imageViewShow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:scaleType="matrix" >
</com.example.hsjgapp.RotateImageView>

<ImageView                        //这里当作点击按钮使用 , 也可以用Button组件
android:id="@+id/rotateImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:gravity="center_horizontal"
android:src="@drawable/rotate_photo">
</ImageView>

RotateImageView document reads as follows:
public class RotateImageView extends ImageView {

    public RotateImageView(Context context) {
        super(context);
    }

    public RotateImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas){

        super.onDraw(canvas);

    }
}

Logic processing document MainActivity.java codes as follows:

String imgShow = localImagePath + theinformation.getJylsh () + "/" + photoType + "_img" + ".jpg"; // image path 
Bitmap bmp = getLoacalBitmap (imgShow); // get the picture Bitmap

ImageView view = (ImageView) findViewById ( R.id.imageViewShow); // get ImageView components 
view.setImageBitmap (bmp); // display Bitmap images on the ImageView components


= RotateImageView the ImageView (the ImageView) findViewById (R.id.rotateImageView); 
rotateImageView.setOnClickListener (new new OnClickListener () {// each click rotate the picture 90 °, and re-display the watermark
int COUNT = 0;
@Override
public void onClick (View View) {
COUNT ++;
BMP = getLoacalBitmap (localTempImgDir); // no watermark FIG

the ImageView Imgview = (the ImageView) the findViewById (R.id.imageViewShow);

// rotate the picture
Bitmap. SetRotate = Rotate (BMP, COUNT * 90);
/ / font added
resultBitmap = addTextWatermark (Rotate, photoType, jyyXm, theinformation.getClsbdh (),
theinformation.getHphm (), theinformation.getHpzl (), theinformation.getJylsh (), to true);

Imgview.setImageBitmap (resultBitmap); // add to rotate and display the photos font

}
});

/ *
  Also keep a photograph to add the font to the specified directory
* /
String ImagePath = localImagePath + theinformation.getJylsh() + "/" + photoType+ "_img" + ".jpg";
File file = new File(ImagePath);
boolean isSuccess = save(resultBitmap,file,true);


/ ** 
* Load local image http://bbs.3gstdy.com
*
* @param URL
* @return
* /
public static Bitmap getLoacalBitmap (String URL) {
the try {
Bitmap BMP = BitmapFactory.decodeFile (URL);
return BMP;
the catch} (Exception E) {
// the TODO Auto-Generated Block the catch
e.printStackTrace ();
return null;
}
}

// picture rotation method

public static Bitmap setRotate (Bitmap map, int rotate) {
    The Matrix Matrix = new new the Matrix (); 
// Set the rotation angle
matrix.setRotate (Rotate);
// redrawn Bitmap
Map = Bitmap.createBitmap (Map, map.getWidth () / 10, 0, map.getWidth (). 8 * /10,map.getHeight (), Matrix, to true);
return Map;
}

/ ** 
* Bitmap to add a watermark text.
* @Param src source images
* // @ param content watermark text
* @param recycle if recovery
* @return Bitmap has been added after the watermark.
* /
Public addTextWatermark Bitmap (Bitmap the src, strName String, String newjyyxm, clsbdh String,
String hphm, hpzl String, String jylsh, Boolean Recycle) {
IF (isEmptyBitmap (the src)) {
return null;
}
String TimeTool.getTiem STR = ( );
string sjimei = "IMEI:"
+ ((TelephonyManager) ImageShowActivity.this
. .getSystemService (TELEPHONY_SERVICE)) GetDeviceID ();
// switch to sjimei string uppercase
StringBuffer sb = new StringBuffer ();
IF (sjimei = null!) {
for (int I = 0; I <sjimei.length (); I ++) {
char sjimei.charAt C = (I);
sb.append (Character.toUpperCase (C));
}
}
= sb.toString sjimei ();
Bitmap RET = src.copy (src.getConfig (), to true);
the Paint Paint new new = the Paint (Paint.ANTI_ALIAS_FLAG);
the Canvas Canvas new new = the Canvas (RET);
paint.setColor (Color. RED);
paint.setTextSize (25.0f);
Rect bounds = new new Rect ();
paint.getTextBounds (strName, 0, strname.length (), bounds);

canvas.drawText ( "photo name:" + strname + "time: "+ str, 15, 25, paint); // draw up a word, began to unknown x, y pen draw would only adopt
canvas.drawText (sjimei +" inspector: "+ newjyyxm, 15, 50 , paint);
canvas.drawText ( "serial number:" + jylsh, 15, 75, Paint);
canvas.drawText ( "plate:" + hphm + "Category:" + hpzl, 15, 100, Paint);
canvas.drawText ( "Model: "+ clsbdh, 15, 125, Paint);

IF (Recycle src.isRecycled && (!)) {
src.recycle ();
}
canvas.save (Canvas.ALL_SAVE_FLAG);
canvas.restore ();
return RET;
}
/ **
* Bitmap object is empty.
* /
Public static Boolean isEmptyBitmap (Bitmap the src) {
return src.getWidth the src == null || () == 0 || src.getHeight () == 0;
}
/ **
* save image file to File.
*
* @Param src source images
* @param path to be saved to a file
* @param // format to save the format (PNG, JPEG, webp)
* @param recycle 是否回收
* @return true 成功 false 失败
*/
public boolean save(Bitmap src, File path, boolean recycle) {
if (isEmptyBitmap(src)) {
return false;
}
OutputStream os;
boolean ret = false;
try {
os = new BufferedOutputStream(new FileOutputStream(path));
ret = src.compress(Bitmap.CompressFormat.JPEG, 100, os);
if (recycle && !src.isRecycled())
src.recycle();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
 

 

Guess you like

Origin www.cnblogs.com/xiobai/p/11671509.html