今天有点事所以今天编码就有点少,把队友的代码考到我的android中然后配置一下相关的属性。明天将今天该做的冲刺回来,然后补下这篇博客
补:
冲刺内容:二维码
参考网址:https://github.com/yipianfengye/android-zxingLibrary
冲刺结果
1:普通扫描框
2:闪光灯
3:拉去本地相册
源代码:
QrCodeActivity.java
package com.example.meet.UI; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import com.example.framework.base.BaseUIActivity; import com.example.framework.helper.FileHelper; import com.example.meet.R; import com.uuzuche.lib_zxing.activity.CaptureFragment; import com.uuzuche.lib_zxing.activity.CodeUtils; public class QrCodeActivity extends BaseUIActivity implements View.OnClickListener { private static final int REQUEST_IMAGE = 1234; private CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() { public void onAnalyzeSuccess(Bitmap mBitmap, String result) { Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS); bundle.putString(CodeUtils.RESULT_STRING, result); resultIntent.putExtras(bundle); setResult(RESULT_OK, resultIntent); finish(); } public void onAnalyzeFailed() { Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED); bundle.putString(CodeUtils.RESULT_STRING, ""); resultIntent.putExtras(bundle); setResult(RESULT_OK, resultIntent); finish(); } }; //返回键 private ImageView iv_back; //相册选择 private TextView iv_to_ablum; //闪光灯 private ImageView iv_flashlight; //是否打开闪光灯 private boolean isOpenLight = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qrcode); initQrCode(); initView(); } /* * 初始化二维码 * */ private void initQrCode() { CaptureFragment captureFragment = new CaptureFragment(); // 为二维码扫描界面设置定制化界面 CodeUtils.setFragmentArgs(captureFragment, R.layout.layout_qrcode); captureFragment.setAnalyzeCallback(analyzeCallback); /** * 替换我们的扫描控件 */ getSupportFragmentManager().beginTransaction() .replace(R.id.fl_my_container, captureFragment).commit(); } /* * 初始化view * * */ private void initView() { iv_back = (ImageView) findViewById(R.id.iv_back); iv_to_ablum = (TextView) findViewById(R.id.iv_to_ablum); iv_flashlight = (ImageView) findViewById(R.id.iv_flashlight); iv_back.setOnClickListener(this); iv_to_ablum.setOnClickListener(this); iv_flashlight.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()) { case R.id.iv_back: finish(); break; case R.id.iv_to_ablum: openAblum(); break; case R.id.iv_flashlight: isOpenLight= !isOpenLight; CodeUtils.isLightEnable(isOpenLight); iv_flashlight.setImageResource(isOpenLight?R.drawable.img_flashlight_p: R.drawable.img_flashlight); break; } } /* * 打开相册 * */ private void openAblum() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(intent, REQUEST_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE) { if (data != null) { Uri uri = data.getData(); String path = FileHelper.getInstance().getRealPathFromURI(QrCodeActivity.this,uri); try { CodeUtils.analyzeBitmap(path, new CodeUtils.AnalyzeCallback() { @Override public void onAnalyzeSuccess(Bitmap mBitmap, String result){ analyzeCallback.onAnalyzeSuccess(mBitmap,result); } @Override public void onAnalyzeFailed() { analyzeCallback.onAnalyzeFailed(); } }); } catch (Exception e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } super.onActivityResult(requestCode, resultCode, data); } }
StartFragment.java
package com.example.meet.fragment; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.example.framework.adapter.CloudTagAapter; import com.example.framework.utils.LogUtils; import com.example.meet.R; import com.example.meet.UI.AddFriendActivity; import com.example.meet.UI.QrCodeActivity; import com.example.meet.UI.UserInfoActivity; import com.moxun.tagcloudlib.view.TagCloudView; import com.uuzuche.lib_zxing.activity.CodeUtils; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class StarFragment extends Fragment { private TextView tvStarTitle; private ImageView ivCamera; private ImageView ivAdd; private TextView tvConnectStatus; private TagCloudView mCloudView; private LinearLayout llRandom; private TextView tvRandom; private LinearLayout llSoul; private TextView tvSoul; private LinearLayout llFate; private TextView tvFate; private LinearLayout llLove; private TextView tvLove; //二维码结果 private static final int REQUEST_CODE=1234; private List<String> list = new ArrayList<>(); private CloudTagAapter cloudTagAapter; public StarFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_star, container, false); initView(view); return view; } private void initView(View view){ tvStarTitle = (TextView) view.findViewById(R.id.tv_star_title); ivCamera = (ImageView) view.findViewById(R.id.iv_camera); ivAdd = (ImageView) view.findViewById(R.id.iv_add); tvConnectStatus = (TextView) view.findViewById(R.id.tv_connect_status); mCloudView = (TagCloudView) view.findViewById(R.id.mCloudView); llRandom = (LinearLayout) view.findViewById(R.id.ll_random); tvRandom = (TextView) view.findViewById(R.id.tv_random); llSoul = (LinearLayout) view.findViewById(R.id.ll_soul); tvSoul = (TextView) view.findViewById(R.id.tv_soul); llFate = (LinearLayout) view.findViewById(R.id.ll_fate); tvFate = (TextView) view.findViewById(R.id.tv_fate); llLove = (LinearLayout) view.findViewById(R.id.ll_love); tvLove = (TextView) view.findViewById(R.id.tv_love); for (int i = 0; i < 50; i++) { list.add("Star " + i); } cloudTagAapter = new CloudTagAapter(getActivity(),list); mCloudView.setAdapter(cloudTagAapter); mCloudView.setOnTagClickListener(new TagCloudView.OnTagClickListener() { @Override public void onItemClick(ViewGroup parent, View view, int position) { Toast.makeText(getContext(),list.get(position),Toast.LENGTH_SHORT).show(); } }); //跳转到添加好友页面 ivAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(getActivity(), AddFriendActivity.class)); } }); ivCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), QrCodeActivity.class); startActivityForResult(intent, REQUEST_CODE); // startActivity(new Intent(getActivity(), QrCodeActivity.class)); } }); } /*public void onClick(View view) { switch (view.getId()) { case R.id.iv_camera: //扫描 Intent intent = new Intent(getActivity(), QrCodeActivity.class); startActivityForResult(intent, REQUEST_CODE); startActivity(new Intent(getActivity(), QrCodeActivity.class)); break; } }*/ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE) { //处理扫描结果(在界面上显示) if (null != data) { Bundle bundle = data.getExtras(); if (bundle == null) { return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); LogUtils.i("result: "+result); if(!TextUtils.isEmpty(result)) { if (result.startsWith("Meet")) { String[] split=result.split("#"); if(split!=null&&split.length>=2) { try { UserInfoActivity.startActivity(getActivity(),split[1]); }catch (Exception e) { e.printStackTrace();; } } } else { Toast.makeText(getActivity(), "二维码错误", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(getActivity(), "二维码错误", Toast.LENGTH_LONG).show(); } } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(getActivity(), "解析二维码失败", Toast.LENGTH_LONG).show(); } } } super.onActivityResult(requestCode, resultCode, data); } }