Java tutorial
package com.example.photoremark; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.provider.MediaStore; import android.support.v4.app.FragmentActivity; import android.telephony.TelephonyManager; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.example.photoremark.poi.VisitActivity; import org.supermap.fm.license.AppLicense; import org.supermap.fm.license.AppLicenseUtil; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends FragmentActivity implements OnClickListener { private ImageView image; private Button takephoto; private String tempFile = ""; public static int PHOTOSUCESS = 1; public static int STORESUCESS = 2; public static int STOREFIELD = 3; public static int RESULT_CAPTURE_IMAGE = 4;// ?? public static int INITOVER = 5; private String deviceIdFile = ""; private SystemInfo systemInfo; private ProgressDialog processDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); systemInfo = (SystemInfo) getApplication(); image = (ImageView) this.findViewById(R.id.image); takephoto = (Button) this.findViewById(R.id.takephoto); takephoto.setOnClickListener(this); takephoto.setClickable(false); findViewById(R.id.create_word_home).setOnClickListener(this); findViewById(R.id.create_word_village).setOnClickListener(this); //???? initresouceFile(); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == PHOTOSUCESS) { String path = (String) msg.obj; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap bm = null; try { bm = scalePicture(path, options, systemInfo.screenWidth, systemInfo.screenWidth); } catch (OutOfMemoryError err) { err.printStackTrace(); } if (bm != null) { image.setScaleType(ImageView.ScaleType.FIT_CENTER); image.setImageBitmap(bm); } if (processDialog != null) { processDialog.cancel(); } } else if (msg.what == STORESUCESS) { if (processDialog != null) { processDialog.cancel(); } Log.d("hzm", "???"); Toast.makeText(getApplication(), "???", Toast.LENGTH_SHORT).show(); } else if (msg.what == STOREFIELD) { if (processDialog != null) { processDialog.cancel(); } Log.d("hzm", "??"); Toast.makeText(getApplication(), "??", Toast.LENGTH_SHORT).show(); } else if (msg.what == INITOVER) { AppLicense applicense = matchingDeviceID(); if (!applicense.isLicenseValid) { takephoto.setClickable(false); // ?? showLicenseDialog(applicense.status); } else { Toast.makeText(MainActivity.this, "???", Toast.LENGTH_SHORT).show(); takephoto.setClickable(true); } } } }; @Override public void onClick(View v) { switch (v.getId()) { case R.id.takephoto: // ??sdcard String status = Environment.getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED)) { try { Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); tempFile = String.valueOf(System.currentTimeMillis()) + ".jpg"; File f = new File(systemInfo.PATH_TEMP, tempFile); Uri u = Uri.fromFile(f); intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); intent.putExtra(MediaStore.EXTRA_OUTPUT, u); startActivityForResult(intent, RESULT_CAPTURE_IMAGE); } catch (ActivityNotFoundException e) { Toast.makeText(MainActivity.this, "", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(MainActivity.this, "?", Toast.LENGTH_LONG).show(); } break; case R.id.create_word_home: Intent intent = new Intent(this, VisitActivity.class); intent.putExtra(VisitActivity.EXTRA, VisitActivity.TO_HOME); startActivity(intent); break; case R.id.create_word_village: Intent intent1 = new Intent(this, VisitActivity.class); intent1.putExtra(VisitActivity.EXTRA, VisitActivity.TO_VILLAGE); startActivity(intent1); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_CAPTURE_IMAGE && (resultCode == Activity.RESULT_OK)) { processDialog = new ProgressDialog(MainActivity.this); processDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // processDialog.setTitle(""); processDialog.setMessage("?..."); // ProgressDialog ??? false ?? processDialog.setIndeterminate(false); // ProgressDialog ??? processDialog.setCancelable(false); processDialog.show(); watermarkphoto(); } } /** * ?id */ private AppLicense matchingDeviceID() { long lasttime = System.currentTimeMillis(); AppLicense applicense = new AppLicense(); File licenseFile = new File(deviceIdFile); if (licenseFile.exists()) { String deviceId = FileUtil.readFile(deviceIdFile).toString(); applicense = AppLicenseUtil.readFMLicense(systemInfo.PATH_LICENSE + "SuperMap FieldMapper Trial.slm", lasttime, deviceId); } else { applicense.status = "APPID?"; } return applicense; } private void initresouceFile() { Thread tr = new Thread() { @Override public void run() { super.run(); // deviceId?? deviceIdFile = systemInfo.PATH_LICENSE + "device.txt"; //TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); //String deviceId = tm.getDeviceId().trim();// ?id //FileUtil.writeFile(deviceIdFile, deviceId);// ,???? handler.sendEmptyMessage(INITOVER); } }; tr.start(); } private void watermarkphoto() { Thread tr = new Thread() { @Override public void run() { super.run(); File f = new File(systemInfo.PATH_TEMP + tempFile); Uri uri = null; try { uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), null, null)); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap bitmap = null; try { bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), uri); } catch (Exception e) { e.printStackTrace(); } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd[HH-mm-ss]"); Date date = new Date(); String str = formatter.format(date.getTime()).replace(" ", "T"); PhotoWatermark photoWatermark = new PhotoWatermark(); String[] longstr = changeDu(systemInfo.naviGps.dLongitude); String[] latstr = changeDu(systemInfo.naviGps.dLatitude); String lon = longstr[0] + "" + longstr[1] + "" + longstr[2] + " "; String lat = latstr[0] + "" + latstr[1] + "" + latstr[2] + " "; String alt = roundDouble(systemInfo.naviGps.dAltitude, 2) + ""; String name = formatter1.format(date.getTime()); String path = systemInfo.PATH_CAMERA + name + ".jpg"; photoWatermark.addWordToBitmap(bitmap, str, lon, lat, alt, handler, path); } }; tr.start(); } private void showLicenseDialog(String content) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("?"); builder.setMessage(content); builder.setCancelable(false); builder.setPositiveButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { android.os.Process.killProcess(android.os.Process.myPid()); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { onExit(); } } return true; } private void onExit() { AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setTitle(""); builder.setMessage("??"); builder.setNegativeButton("?", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).setPositiveButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { android.os.Process.killProcess(android.os.Process.myPid()); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } public boolean isGpsDataValid(double dx, double dy) { boolean bResult = false; if (dx >= 0.00000001 || dx <= -0.00000001 || dy >= 0.00000001 || dy <= -0.00000001) { bResult = true; } return bResult; } /** * */ public String[] changeDu(double dx) { int d = (int) dx;// double f = (dx - d) * 60; double tm = (f - (int) f) * 60; double m = Math.round(tm * 100) / 100.0; String[] str = new String[3]; str[0] = d + ""; str[1] = (int) f + ""; str[2] = m + ""; return str; } /** * ? * * @param filepath ? * @param options BitmapFactory.Options * @return */ public Bitmap scalePicture(String filepath, BitmapFactory.Options options, int screenWidth, int scaleHeight) { // ? int rotate = getPictureDegree(filepath); Bitmap bm = null; try { bm = BitmapFactory.decodeFile(filepath, options); } catch (OutOfMemoryError err) { } // ? if (bm != null) { int width = bm.getWidth(); int height = bm.getHeight(); // float scaleWidth = ((float) screenWidth) / width; Matrix matrix = new Matrix(); // matrix.postScale(scaleWidth, scaleWidth); // matrix.postRotate(rotate); bm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); } return bm; } /** * ?exif? * * @param path * @return */ public int getPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } @Override protected void onDestroy() { super.onDestroy(); if (processDialog != null) { processDialog.cancel(); } } public double roundDouble(double value, int weishu) { BigDecimal bg = new BigDecimal(value); double newValue = bg.setScale(weishu, BigDecimal.ROUND_HALF_UP).doubleValue(); return newValue; } }