Android无法更改字段值

时间:2018-03-25 05:57:24

标签: java android opencv

comment
  reply
  reply
     reply
       reply
  reply
comment
  reply
  reply

[此来源是所有来源的一部分]

现在我遇到了一些问题。我将操作放在菜单按钮中并处理它。 id.action_resolution是完美的。但问题是id.action_naming。 我想更改使用文件名的字段值[studentId]。 但无论我做什么,价值都不会改变。 值吐司的输出只是空白。 变量声明和ID分配是完美的。 此菜单侦听器在onCreate()中声明 我怎样才能改变Field的价值?

fabSpeedDial.setMenuListener(new FabSpeedDial.MenuListener() {
        @Override
        public boolean onPrepareMenu(NavigationMenu navigationMenu) {
            return true; //false: 메뉴를 보여주지않는다.
        }

        @Override
        public boolean onMenuItemSelected(MenuItem menuItem) {
            if(menuItem.getItemId()== id.action_resolution){
                AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                resolutionView = (View)View.inflate(MainActivity.this, R.layout.listview, null);
                listView = (ListView)resolutionView.findViewById(R.id.listView);
                listView.setAdapter(resolutionAdapter);
                listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                dlg.setTitle("해상도 설정");
                dlg.setView(resolutionView);
                dlg.setPositiveButton("설정", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int count = listView.getCheckedItemPosition();
                        String temp=resolutionAdapter.getItem(count);
                        String[] whString=temp.split("x",2);
                        int height=Integer.parseInt(whString[1]);
                        int width=Integer.parseInt(whString[0]);
                        mOpenCvCameraView.setMinimumHeight(height); // 카메라 해상도 관련 처리
                        mOpenCvCameraView.setMinimumWidth(width);
                        mOpenCvCameraView.setMaxFrameSize(width,height);
                        mOpenCvCameraView.disableView();
                        mOpenCvCameraView.enableView();
                        listView.setItemChecked(count,true);
                        Toast.makeText(getApplicationContext(),"설정되었습니다.",Toast.LENGTH_LONG).show();
                    }
                });
                dlg.show();

            }
            if(menuItem.getItemId()==id.action_naming){
                AlertDialog.Builder noticeDialog = new AlertDialog.Builder(MainActivity.this);
                //사용방법 관련
                noticeDialog.setTitle("사용 방법");
                noticeDialog.setMessage("휴대폰을 가로 방향으로 들고 번호판이 화면에 감지 될 경우 캡쳐됩니다.\n" +
                        "내장메모리의 CarPlate 폴더 안에 png 파일로 저장됩니다.\n");
                noticeDialog.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String id = edtNumber.getText().toString();
                        studentId = id;
                        Toast.makeText(getApplicationContext(),studentId,Toast.LENGTH_LONG).show();
                    }
                });
                noticeDialog.setView(layout.inputnumber);
                noticeDialog.show();
            }
            return true;
        }

        @Override
        public void onMenuClosed() {

        }
    });

...

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_resolution"
        android:icon="@drawable/ic_resolution"
        android:title="해상도 조절"
        />
    <item
        android:id="@+id/action_naming"
        android:icon="@drawable/ic_naming_file"
        android:title="파일명 설정"/>
</menu>

这是我的onCreate()。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(layout.activity_main);
        camera = Camera.open();
        final Camera.Parameters parameters = camera.getParameters();
        mPlayer = MediaPlayer.create(this, R.raw.dingdong);
        mPlayer.setLooping(false);
        errorPlayer = MediaPlayer.create(this, raw.save_error);
        errorPlayer.setLooping(false);
        resolutionList = new ArrayList<String>();
        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(id.activity_surface_view);
        edtNumberView = (View)View.inflate(MainActivity.this, layout.inputnumber, null);
        edtNumber = (EditText)edtNumberView.findViewById(R.id.edtNumber);

        for(Camera.Size size : parameters.getSupportedPictureSizes())
            resolutionList.add(size.width+"x"+size.height);
        //학번으로 파일 명 설정하게 해주는 작업




        Log.d(TAG, "onCreate starts");
        final String dirPath=Environment.getExternalStorageDirectory().getAbsolutePath();
        final File myDir=new File(dirPath+"/CarPlate");
        if(!myDir.exists()){
            myDir.mkdirs();
            Toast.makeText(getApplicationContext(),"내장 메모리에 CarPlate 폴더가 생성되었습니다.",Toast.LENGTH_LONG).show();
        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //퍼미션 상태 확인
            if (!hasPermissions(PERMISSIONS)) {
                //퍼미션 허가 안되어있다면 사용자에게 요청
                requestPermissions(PERMISSIONS, PERMISSIONS_REQUEST_CODE);
            }
        }
        ///

        ///해상도 설정 관련
        resolutionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, resolutionList);
        FabSpeedDial fabSpeedDial =  (FabSpeedDial)findViewById(id.fabSpeedDial);
        fabSpeedDial.setMenuListener(new FabSpeedDial.MenuListener() {
            @Override
            public boolean onPrepareMenu(NavigationMenu navigationMenu) {
                return true; //false: 메뉴를 보여주지않는다.
            }

            @Override
            public boolean onMenuItemSelected(MenuItem menuItem) {
                if(menuItem.getItemId()== id.action_resolution){
                    AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                    resolutionView = (View)View.inflate(MainActivity.this, R.layout.listview, null);
                    listView = (ListView)resolutionView.findViewById(R.id.listView);
                    listView.setAdapter(resolutionAdapter);
                    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    dlg.setTitle("해상도 설정");
                    dlg.setView(resolutionView);
                    dlg.setPositiveButton("설정", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            int count = listView.getCheckedItemPosition();
                            String temp=resolutionAdapter.getItem(count);
                            String[] whString=temp.split("x",2);
                            int height=Integer.parseInt(whString[1]);
                            int width=Integer.parseInt(whString[0]);
                            mOpenCvCameraView.setMinimumHeight(height); // 카메라 해상도 관련 처리
                            mOpenCvCameraView.setMinimumWidth(width);
                            mOpenCvCameraView.setMaxFrameSize(width,height);
                            mOpenCvCameraView.disableView();
                            mOpenCvCameraView.enableView();
                            listView.setItemChecked(count,true);
                            Toast.makeText(getApplicationContext(),"설정되었습니다.",Toast.LENGTH_LONG).show();
                        }
                    });
                    dlg.show();

                }
                if(menuItem.getItemId()==R.id.action_naming){
                    AlertDialog.Builder noticeDialog = new AlertDialog.Builder(MainActivity.this);
                    //사용방법 관련
                    noticeDialog.setTitle("사용 방법");
                    noticeDialog.setMessage("휴대폰을 가로 방향으로 들고 번호판이 화면에 감지 될 경우 캡쳐됩니다.\n" +
                            "내장메모리의 CarPlate 폴더 안에 png 파일로 저장됩니다.\n");
                    noticeDialog.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String id = edtNumber.getText().toString();
                            studentId = id;
                            Toast.makeText(getApplicationContext(),studentId,Toast.LENGTH_LONG).show();
                        }
                    });
                    noticeDialog.setView(layout.inputnumber);
                    noticeDialog.show();
                }
                return true;
            }

            @Override
            public void onMenuClosed() {

            }
        });
        mOpenCvCameraView.setMinimumHeight(1080); // 카메라 해상도 관련 처리
        mOpenCvCameraView.setMinimumWidth(1920);
        mOpenCvCameraView.setMaxFrameSize(1920,1080);
        mOpenCvCameraView.disableView();
        mOpenCvCameraView.enableView();
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(MainActivity.this);
        mOpenCvCameraView.setCameraIndex(0); // front-camera(1),  back-camera(0)
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);

/*
        //===================================================================================
        // 테스트 위해 파일에서 영상 읽어들임
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), drawable.test_image_800);
        Log.d(TAG, "  bmp read. " + bmp.getHeight() + ", " + bmp.getWidth());
        Toast.makeText(this, "bmp:("+bmp.getHeight()+","+bmp.getWidth()+")",Toast.LENGTH_LONG).show();
        Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
        matTemp = new Mat();
        Utils.bitmapToMat(bmp32, matTemp);
        Log.d(TAG, "  bmp2Mat : " + matTemp.rows() + ", " + matTemp.cols());
//==================================================================
*/


        Log.d(TAG, "onCreate ends");
    }

这种方法是检测车牌。 专注于saveInternalStorage()

public Mat onCameraFrame(final CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        matInput = inputFrame.rgba();
        Log.d(TAG, "onCameraFrame starts.  matInput.rows : " + matInput.rows());
        if (matResult != null) matResult.release();
        matResult = new Mat(matInput.size(), matInput.type());
        resultStr = PlateRecogCpp(matInput.getNativeObjAddr(), matResult.getNativeObjAddr());

        Log.d(TAG, "onCameraFrame. PlateRecogCpp done. resultStr" + resultStr);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (resultStr.charAt(resultStr.length() - 1) == '*') { //검출 성공 시에
                    newSecond = Integer.parseInt(new SimpleDateFormat("ss").format(new Date()));
                    if (prevSecond == null ||Math.abs(newSecond - prevSecond) > 3) {
                        Bitmap bmp = null;
                        try {
                            bmp = Bitmap.createBitmap(matInput.cols(), matInput.rows(), Bitmap.Config.ARGB_8888);
                            Utils.matToBitmap(matInput, bmp);
                            saveToInternalStorage(bmp, studentId);
                        }
                        catch (CvException e) {
                            Log.d(TAG, e.getMessage());
                        }
                        catch(Exception e){
                                errorPlayer.start();
                                Toast.makeText(getApplicationContext(),"용량이 가득찼습니다. 용량을 확보해주세요.", Toast.LENGTH_LONG).show();
                                Log.e(TAG, e.getMessage());

                        }
                        mPlayer.start();
                        prevSecond = newSecond;
                        Toast.makeText(getApplicationContext(), "CarPlate에 저장 완료", Toast.LENGTH_LONG).show();
                    }
 //                   Imgcodecs.imwrite("savedImage.jpg", matInput);
                }
            }
        });

        return matResult;
    }

它全部......

0 个答案:

没有答案