【问题标题】:Scan QR Code and take picture QR Code at same time?同时扫描二维码和拍照二维码?
【发布时间】:2021-08-30 03:18:42
【问题描述】:

我有一个使用 ZXing 库的二维码扫描仪,现在我只需要保存图像中的二维码。 我尝试拍照,但总是参加不同的活动。像扫描活动与拍照活动不同。所以我必须做2次才能获取二维码。

  1. 扫描二维码
  2. 拍照。 我只想做 1 次,而不是拍照活动。

我正在尝试使用这个cameraIntent.putExtra("android.intent.extra.quickCapture", true);,但它看起来像正常拍照。

我还发现了一个类似于我现在所做的应用程序。 二维码和条码扫描仪

从此:

到这里:

我想做的是扫描二维码,只裁剪二维码部分,然后向数据库发送请求。 或者无论如何要这样做并提供帮助,非常感谢。

【问题讨论】:

    标签: android qr-code


    【解决方案1】:

    扫描二维码,同时拍照二维码

    如果您的意思是扫描二维码并将二维码显示为图片,那么您可以按照答案进行操作。

    通过integrator.initiateScan();扫描二维码,然后在onActivityResult上通过MultiFormatWriterBarcodeEncoder创建结果的位图,并在任何ImageView中显示位图并在任何TextView中显示。

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                try {
                    BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), BarcodeFormat.QR_CODE, 200, 200);
                    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                    Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                    image.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    e.printStackTrace();
                }
    

    根据您附加的图片,我将分享一个完整的活动和一个布局文件,这将满足您的要求。 这将扫描二维码,将结果显示为图像和文本。如果结果包含任何 url ,那么它将使其可点击 Linkify 。通过ClipboardManager复制结果添加了几个按钮,通过Intent分享结果。

    receive.xml

     <?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="10dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linear1"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/openButton"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="50dp"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:text="Open"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textStyle="normal" />
    
            <Button
                android:id="@+id/searchButton"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="50dp"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:text="Search"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textStyle="normal" />
    
            <Button
                android:id="@+id/copyButton"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="50dp"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:text="Copy"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textStyle="normal" />
    
            <Button
                android:id="@+id/shareButton"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginTop="50dp"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:text="Share"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:textStyle="normal" />
    
        </LinearLayout>
    
            <TextView
            android:id="@+id/someText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="120dp"
            android:gravity="center"
            android:textColor="@android:color/black" />
    
         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:layout_below="@+id/someText">
    
             <ImageView
                 android:id="@+id/image"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent" />
    
        </LinearLayout>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="520dp"
            android:layout_centerHorizontal="true"
            android:text="SCAN AGAIN"
            android:theme="@style/ConnectButton"
            android:id="@+id/scan_btn"/>
    </RelativeLayout>
    

    接收活动 -

    public class ReceiveS extends AppCompatActivity {
    public static Button scan_btn = null;
    ImageView image;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receive);
    
        image = (ImageView) findViewById(R.id.image);
    
        // show custom alert dialog
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.receive);
    
    
        TextView text = (TextView) findViewById(R.id.someText);
        final Activity activity = this;
    
        scan_btn = (Button) findViewById(R.id.scan_btn);
        scan_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(false);
                integrator.initiateScan();
            }
        });
    }
    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        TextView text = (TextView) findViewById(R.id.someText);
    
    
        final IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "You cancelled scanning", Toast.LENGTH_LONG).show();
            } else {
    
                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                try {
                    BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), BarcodeFormat.QR_CODE, 200, 200);
                    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                    Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                    image.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    e.printStackTrace();
                }
    
                SpannableString url = SpannableString.valueOf(result.getContents());
                Linkify.addLinks(url, Linkify.WEB_URLS);
                text.setText(url);
                text.setMovementMethod(LinkMovementMethod.getInstance());
    
    
                Button openlink = (Button) findViewById(R.id.openButton);
                Button webSearch = (Button) findViewById(R.id.searchButton);
                Button copy = (Button) findViewById(R.id.copyButton);
                Button share = (Button) findViewById(R.id.shareButton);
    
                openlink.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url = result.getContents();
                        if (url.startsWith("https://") || url.startsWith("http://")) {
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                            startActivity(intent);
                        } else {
                            Toast.makeText(ReceiveS.this, "Invalid Url", Toast.LENGTH_SHORT).show();
                        }
    
                    }
                });
    
                webSearch.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url;
                        url = "http://www.google.com/#q=" + result.getContents();
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                    }
                });
                copy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("", result.getContents());
                        clipboard.setPrimaryClip(clip);
    
                        Toast.makeText(getApplicationContext(), "Copied to clipboard", Toast.LENGTH_SHORT).show();
    
                    }
                });
                share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                        sharingIntent.setType("text/plain");
                        String shareBody = result.getContents();
                        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                        startActivity(Intent.createChooser(sharingIntent, "Share "));
    
                    }
                });
    
    
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
      }
    

    【讨论】:

    • 很好,我已经坚持了一个月。希望这会奏效。谢谢
    • 所以,它从结果中创建新的条形码。而且它不是裁剪图片。我的应用需要拍照而不是编码新的,我的合作伙伴创建带有加密的自定义二维码。但感谢您的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    相关资源
    最近更新 更多