Android實(shí)現(xiàn)手勢(shì)劃定區(qū)域裁剪圖片
本文實(shí)例為大家分享了Android實(shí)現(xiàn)手勢(shì)劃定區(qū)域裁剪圖片的具體代碼,供大家參考,具體內(nèi)容如下
需求:
拍照,然后對(duì)圖片進(jìn)行處理,劃定矩形區(qū)域,將矩形區(qū)域裁剪下來
思路:
1、使用系統(tǒng)相機(jī)拍照,拍完返回,對(duì)圖片進(jìn)行壓縮和存儲(chǔ)。
2、新建一個(gè)activity處理圖片裁剪,利用自定義view在畫布上畫出矩形區(qū)域。
3、根據(jù)坐標(biāo)信息生成裁剪圖片并存儲(chǔ)。
部分核心代碼:
1、調(diào)用系統(tǒng)相機(jī)拍照
String IMAGE_PATH = Environment.getExternalStorageDirectory().getPath()+ "/com.kwmax.demo/Image/"; String filename = "xxxxxx.jpeg"; File picFile = new File(IMAGE_PATH + filename); if (!picFile.exists()) { ? ? picFile.createNewFile(); } ? ... if(getContext().getPackageManager().getLaunchIntentForPackage("com.sec.android.app.camera") != null) { ?? ?cameraIntent.setPackage("com.sec.android.app.camera"); } if (getContext().getPackageManager().getLaunchIntentForPackage("com.android.hwcamera") != null) { ?? ?cameraIntent.setPackage("com.android.hwcamera"); } if (getContext().getPackageManager().getLaunchIntentForPackage("com.zte.camera") != null) { ?? ?cameraIntent.setPackage("com.zte.camera"); } cameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); // 默認(rèn)系統(tǒng)相機(jī) cameraIntent.addCategory("android.intent.category.DEFAULT"); ? Uri pictureUri = Uri.fromFile(picFile); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); startActivityForResult(intent, CAMERA_REQUEST_CODE);
2、自定義手勢(shì)矩形view
public class CaptureRectView extends View { ? ? private int x; ? ? private int y; ? ? private int m; ? ? private int n; ? ? private boolean sign;//繪畫標(biāo)記位 ? ? private Paint paint;//畫筆 ? ? ? public CaptureRectView (Context context) { ? ? ? ? super(context); ? ? ? ? paint = new Paint(Paint.FILTER_BITMAP_FLAG); ? ? } ? ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? if(sign){ ? ? ? ? ? ? paint.setColor(Color.TRANSPARENT); ? ? ? ? }else{ ? ? ? ? ? ? paint.setColor(Color.RED); ? ? ? ? ? ? paint.setAlpha(80); ? ? ? ? ? ? paint.setStyle(Paint.Style.STROKE); ? ? ? ? ? ? paint.setStrokeWidth(15f); ? ? ? ? ? ? canvas.drawRect(new Rect(x, y, m, n), paint); ? ? ? ? } ? ? ? ? super.onDraw(canvas); ? ? } ? ? ? public void setSeat(int x,int y,int m,int n){ ? ? ? ? this.x = x; ? ? ? ? this.y = y; ? ? ? ? this.m = m; ? ? ? ? this.n = n; ? ? } ? ? ? public boolean isSign() { ? ? ? ? return sign; ? ? } ? ? ? public void setSign(boolean sign) { ? ? ? ? this.sign = sign; ? ? } }
3、裁剪頁面布局
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? ? ? ? ? ? ? android:id="@+id/drawrect_framelayout" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:background="@color/black" ? ? ? ? ? ? ? ? android:clickable="true" ? ? ? ? ? ? ? ? android:orientation="vertical"> ? ? ? <RelativeLayout ? ? ? ? android:id="@+id/drawrect_relativelayout" ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:background="@color/black" ? ? ? ? android:orientation="vertical"> ? ? ? ? ? <FrameLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:layout_above="@+id/bottom"> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:id="@+id/image_zoom_view_layout" ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_centerInParent="true" ? ? ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? ? ? android:orientation="vertical"/> ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:id="@+id/capture_preview" ? ? ? ? ? ? ? ? android:layout_width="80dp" ? ? ? ? ? ? ? ? android:layout_height="80dp"/> ? ? ? ? </FrameLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/bottom" ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="50dip" ? ? ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? ? ? android:orientation="horizontal" ? ? ? ? ? ? android:layout_marginLeft="10dp" ? ? ? ? ? ? android:layout_marginRight="10dp"> ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn_capture" ? ? ? ? ? ? ? ? android:layout_width="50dp" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? ? ? ? ? android:text="裁剪"/> ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/btn_cancel" ? ? ? ? ? ? ? ? android:layout_width="50dp" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="取消"/> ? ? ? ? ? </LinearLayout> ? ? ? </RelativeLayout> ? </FrameLayout>
4、裁剪activity
public class DrawRectActivity extends BasicActivity implements OnClickListener, View.OnTouchListener { ? ? ? private String TAG = "DrawRectActivity"; ? ? private String imageString; ? ? private String imagePath; ? ? private ArrayList<String> imageList = null; ? ? private int position = 0; ? ? private int width, height; ? ? private LinearLayout layerViewLayout = null; ? ? ? private ImageView aiPreview; ? ? private CaptureRectView captureView;//繪畫選擇區(qū)域 ? ? private int capX;//繪畫開始的橫坐標(biāo) ? ? private int capY;//繪畫開始的縱坐標(biāo) ? ? private int capM;//繪畫結(jié)束的橫坐標(biāo) ? ? private int capN;//繪畫結(jié)束的縱坐標(biāo) ? ? private Bitmap captureBitmap; ? ? ? private Button cancel; ? ? private Button aiCapture; ? ? ? private FrameLayout frameLayout; ? ? private RelativeLayout relativeLayout; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? init(); ? ? ? ? initUI(); ? ? } ? ? ? private void init() { ? ? ? ? width = ImageUtils.getScreenWidth(this); ? ? ? ? height = ImageUtils.getScreenHeight(this); ? ? ? ? Intent intent = this.getIntent(); ? ? ? ? Bundle bundle = intent.getExtras(); ? ? ? ? imageString = bundle.getString("imageString"); ? ? ? ? imagePath = bundle.getString("imagePath"); ? ? ? ? position = bundle.getInt("position"); ? ? ? ? imageList = parseImageString(imagePath, imageString); ? ? ? } ? ? ? @TargetApi(Build.VERSION_CODES.JELLY_BEAN) ? ? private void initUI() { ? ? ? ? setContentView(R.layout.draw_image_rect_view); ? ? ? ? frameLayout = (FrameLayout) findViewById(R.id.drawrect_framelayout); ? ? ? ? relativeLayout = (RelativeLayout) findViewById(R.id.drawrect_relativelayout); ? ? ? ? layerViewLayout = (LinearLayout) this.findViewById(R.id.image_zoom_view_layout); ? ? ? ? btncancel = (Button) findViewById(R.id.btn_cancel); ? ? ? ? btnCapture = (Button) findViewById(R.id.btn_capture); ? ? ? ? btnPreview = (ImageView) findViewById(R.id.capture_preview); ? ? ? ? ? ImageView originImage = new ImageView(this); ? ? ? ? Bitmap image = ImageUtils.getBitmapFromFile(imagePath + imageList.get(position), 1); ? ? ? ? originImage.setImageBitmap(image); ? ? ? ? originImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ? ? ? ? layerViewLayout.addView(originImage, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ? ? ? ? ? captureView = new CaptureRectView(this); ? ? ? ? originImage.setOnTouchListener(this); ? ? ? ? this.addContentView(captureView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ? ? ? ? ? if (frameLayout.isClickable()) { ? ? ? ? ? ? frameLayout.setOnClickListener(new OnClickListener() { ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? ? } ? ? ? ? btncancel.setOnClickListener(this); ? ? ? ? btnCapture.setOnClickListener(this); ? ? ? } ? ? ? private ArrayList<String> parseImageString(String imagePath, String imageString) { ? ? ? ? ArrayList<String> list = new ArrayList<String>(); ? ? ? ? String allFiles = imageString.substring(imageString.indexOf("img://") + "img://".length()); ? ? ? ? String fileName = null; ? ? ? ? while (allFiles.indexOf(";") > 0) { ? ? ? ? ? ? fileName = allFiles.substring(0, allFiles.indexOf(";")); ? ? ? ? ? ? allFiles = allFiles.substring(allFiles.indexOf(";") + 1); ? ? ? ? ? ? if (checkIsImageFile(fileName) && new File(imagePath + fileName).exists()) { ? ? ? ? ? ? ? ? list.add(fileName); ? ? ? ? ? ? ? ? Log.v("ParseImageString()", "imageName=" + fileName); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? Log.v("ParseImageString()", "bad imageName=" + fileName); ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? Log.v("ParseImageString()", "imagelist.size=" + list.size()); ? ? ? ? return list; ? ? } ? ? ? /** ? ? ?* 判斷是否相應(yīng)的圖片格式 ? ? ?*/ ? ? private boolean checkIsImageFile(String fName) { ? ? ? ? boolean isImageFormat; ? ? ? ? if (fName.endsWith(".jpg") || fName.endsWith(".gif") || fName.endsWith(".png") || fName.endsWith(".jpeg") || fName.endsWith(".bmp")) { ? ? ? ? ? ? isImageFormat = true; ? ? ? ? } else { ? ? ? ? ? ? isImageFormat = false; ? ? ? ? } ? ? ? ? return isImageFormat; ? ? } ? ? ? @Override ? ? public boolean onTouch(View view, MotionEvent event) { ? ? ? ? switch (event.getAction()) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? capX = 0; ? ? ? ? ? ? ? ? capY = 0; ? ? ? ? ? ? ? ? width = 0; ? ? ? ? ? ? ? ? height = 0; ? ? ? ? ? ? ? ? capX = (int) event.getX(); ? ? ? ? ? ? ? ? capY = (int) event.getY(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? capM = (int) event.getX(); ? ? ? ? ? ? ? ? capN = (int) event.getY(); ? ? ? ? ? ? ? ? captureView.setSeat(capX, capY, capM, capN); ? ? ? ? ? ? ? ? captureView.postInvalidate(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? if (event.getX() > capX) { ? ? ? ? ? ? ? ? ? ? width = (int) event.getX() - capX; ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? width = (int) (capX - event.getX()); ? ? ? ? ? ? ? ? ? ? capX = (int) event.getX(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (event.getY() > capY) { ? ? ? ? ? ? ? ? ? ? height = (int) event.getY() - capY; ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? height = (int) (capY - event.getY()); ? ? ? ? ? ? ? ? ? ? capY = (int) event.getY(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? captureBitmap = getCapturePreview(this); ? ? ? ? ? ? ? ? if (null != captureBitmap) { ? ? ? ? ? ? ? ? ? ? btnPreview.setImageBitmap(captureBitmap); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? if (captureView.isSign()) { ? ? ? ? ? ? return false; ? ? ? ? } else { ? ? ? ? ? ? return true; ? ? ? ? } ? ? } ? ? ? private Bitmap getCapturePreview(Activity activity) { ? ? ? ? View view = activity.getWindow().getDecorView(); ? ? ? ? view.setDrawingCacheEnabled(true); ? ? ? ? view.buildDrawingCache(); ? ? ? ? Bitmap bitmap = view.getDrawingCache(); ? ? ? ? Rect frame = new Rect(); ? ? ? ? activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); ? ? ? ? int toHeight = frame.top; ?? ??? ?//todo:這里需要針對(duì)部分機(jī)型做適配 ? ? ? ? if (width > 0 && height > 0) { ? ? ? ? ? ? bitmap = Bitmap.createBitmap(bitmap, capX, capY + 240, width, height); ? ? ? ? ? ? view.setDrawingCacheEnabled(false); ? ? ? ? ? ? return bitmap; ? ? ? ? } else { ? ? ? ? ? ? return null; ? ? ? ? } ? ? } ? ? ? @Override ? ? public void onClick(View v) { ? ? ? ? ? switch (v.getId()) { ? ? ? ? ? ? case R.id.btn_cancel: ? ? ? ? ? ? ? ? Intent cancelintent = getIntent(); ? ? ? ? ? ? ? ? createPendingResult(600, cancelintent, PendingIntent.FLAG_UPDATE_CURRENT); ? ? ? ? ? ? ? ? setResult(RESULT_OK, cancelintent); ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.btn_capture: ? ? ? ? ? ? ? ? Intent sureintent = getIntent(); ? ? ? ? ? ? ? ? createPendingResult(CpAIphotoAttributes.PHOTO_CAPTURE, sureintent, PendingIntent.FLAG_UPDATE_CURRENT); ? ? ? ? ? ? ? ? if (captureBitmap != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? String file = IMAGE_PATH; ? ? ? ? ? ? ? ? ? ? ? ? String randomid = UUID.randomUUID().toString(); ? ? ? ? ? ? ? ? ? ? ? ? String filename = randomid+ ".jpeg"; ? ? ? ? ? ? ? ? ? ? ? ? FileOutputStream fout = new FileOutputStream(file+filename); ? ? ? ? ? ? ? ? ? ? ? ? captureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout); ? ? ? ? ? ? ? ? ? ? ? ? sureintent.putExtra("capturePath", file+filename); ? ? ? ? ? ? ? ? ? ? ? ? sureintent.putExtra("capturefilename", filename); ? ? ? ? ? ? ? ? ? ? ? ? sureintent.putExtra("capturefileid", randomid); ? ? ? ? ? ? ? ? ? ? } catch (FileNotFoundException e) { ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? setResult(RESULT_OK, sureintent); ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? ? ? @Override ? ? public boolean onKeyDown(int keyCode, KeyEvent event) { ? ? ? ? switch (keyCode) { ? ? ? ? ? ? case KeyEvent.KEYCODE_BACK: ? ? ? ? ? ? ? ? Intent cancelintent = getIntent(); ? ? ? ? ? ? ? ? createPendingResult(600, cancelintent, PendingIntent.FLAG_UPDATE_CURRENT); ? ? ? ? ? ? ? ? cancelintent.putExtra("imagePath", imagePath); ? ? ? ? ? ? ? ? cancelintent.putExtra("position", position); ? ? ? ? ? ? ? ? cancelintent.putExtra("todowhat", "cancel"); ? ? ? ? ? ? ? ? setResult(RESULT_OK, cancelintent); ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return false; ? ? } ? ? ? @Override ? ? public void finish() { ? ? ? ? super.finish(); ? ? } ? ? ? @Override ? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) { ? ? ? ? ? if (resultCode == RESULT_OK) { ? ? ? ? ? ? switch (requestCode) { ? ? ? ? ? ? ? ? case 400: ? ? ? ? ? ? ? ? ? ? String text = null; ? ? ? ? ? ? ? ? ? ? Log.v("DrawRectActivity", "onActivityReault imagePath=" + imagePath + imageList.get(position)); ? ? ? ? ? ? ? ? ? ? if (StringUtil.isNotBlank(text)) { ? ? ? ? ? ? ? ? ? ? ? ? Log.v("DrawRectActivity", "onActivityReault imagePath=" + imagePath + imageList.get(position) + ";text=" + text); ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? super.onActivityResult(requestCode, resultCode, data); ? ? } ? ? ? @Override ? ? protected void onDestroy() { ? ? ? ? super.onDestroy(); ? ? } ? }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決
這篇文章主要介紹了Android studio 4.1打包失敗和插件錯(cuò)誤提示的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android 拍照并對(duì)照片進(jìn)行裁剪和壓縮實(shí)例詳解
這篇文章主要介紹了Android 拍照并對(duì)照片進(jìn)行裁剪和壓縮實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼,需要的朋友可以參考下2017-07-07Android app啟動(dòng)時(shí)黑屏或者白屏的原因及解決辦法
這篇文章主要介紹了Android app啟動(dòng)時(shí)黑屏或者白屏的原因及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-09-09解決android studio 3.0 加載項(xiàng)目過慢問題--maven倉庫選擇
這篇文章主要介紹了android studio 3.0 加載項(xiàng)目過慢問題解決方案---maven倉庫選擇,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11Android Zipalign工具優(yōu)化Android APK應(yīng)用
本文主要介紹Android Zipalign工具優(yōu)化Android APK應(yīng)用,這里整理了相關(guān)資料及簡(jiǎn)單優(yōu)化實(shí)例,有需要的小伙伴可以參考下2016-09-09Android Room數(shù)據(jù)庫容易遇到的問題以及解決方法
這篇文章給大家介紹了我們?cè)贏ndroid Room數(shù)據(jù)庫容易遇到的坑以及解決方法,文中有詳細(xì)的代碼示例供我們參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-09-09